duke@1: /*
mcimadamore@1186: * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1: *
duke@1: * This code is free software; you can redistribute it and/or modify it
duke@1: * under the terms of the GNU General Public License version 2 only, as
ohair@554: * published by the Free Software Foundation. Oracle designates this
duke@1: * particular file as subject to the "Classpath" exception as provided
ohair@554: * by Oracle in the LICENSE file that accompanied this code.
duke@1: *
duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1: * version 2 for more details (a copy is included in the LICENSE file that
duke@1: * accompanied this code).
duke@1: *
duke@1: * You should have received a copy of the GNU General Public License version
duke@1: * 2 along with this work; if not, write to the Free Software Foundation,
duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1: *
ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554: * or visit www.oracle.com if you need additional information or have any
ohair@554: * questions.
duke@1: */
duke@1:
duke@1: package com.sun.tools.javac.comp;
duke@1:
mcimadamore@1114: import com.sun.tools.javac.api.Formattable.LocalizedString;
duke@1: import com.sun.tools.javac.code.*;
mcimadamore@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@1337: import com.sun.tools.javac.comp.Infer.InferenceContext.FreeTypeListener;
mcimadamore@1215: import com.sun.tools.javac.comp.Resolve.MethodResolutionContext.Candidate;
duke@1: import com.sun.tools.javac.jvm.*;
duke@1: import com.sun.tools.javac.tree.*;
mcimadamore@1114: import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@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@1393: import java.util.Set;
mcimadamore@1114:
mcimadamore@1114: import javax.lang.model.element.ElementVisitor;
duke@1:
duke@1: import static com.sun.tools.javac.code.Flags.*;
jjg@1127: import static com.sun.tools.javac.code.Flags.BLOCK;
duke@1: import static com.sun.tools.javac.code.Kinds.*;
jjg@1127: import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
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;
duke@1: public final boolean boxingEnabled; // = source.allowBoxing();
duke@1: public final boolean varargsEnabled; // = source.allowVarargs();
mcimadamore@674: public final boolean allowMethodHandles;
mcimadamore@1393: public final boolean allowDefaultMethods;
mcimadamore@1510: public final boolean allowStructuralMostSpecific;
duke@1: private final boolean debugResolve;
mcimadamore@1114: final EnumSet verboseResolutionMode;
duke@1:
mcimadamore@674: Scope polymorphicSignatureScope;
mcimadamore@674:
mcimadamore@1215: protected Resolve(Context context) {
mcimadamore@1215: context.put(resolveKey, this);
mcimadamore@1215: syms = Symtab.instance(context);
mcimadamore@1215:
mcimadamore@1215: varNotFound = new
mcimadamore@1215: SymbolNotFoundError(ABSENT_VAR);
mcimadamore@1215: methodNotFound = new
mcimadamore@1215: SymbolNotFoundError(ABSENT_MTH);
mcimadamore@1215: typeNotFound = new
mcimadamore@1215: SymbolNotFoundError(ABSENT_TYP);
mcimadamore@1215:
mcimadamore@1215: names = Names.instance(context);
mcimadamore@1215: log = Log.instance(context);
mcimadamore@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@1215: verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
mcimadamore@1215: Target target = Target.instance(context);
mcimadamore@1215: allowMethodHandles = target.hasMethodHandles();
mcimadamore@1393: allowDefaultMethods = source.allowDefaultMethods();
mcimadamore@1510: allowStructuralMostSpecific = source.allowStructuralMostSpecific();
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;
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;
mcimadamore@1215: ListBuffer subDiags = ListBuffer.lb();
mcimadamore@1215: for (Candidate c : currentResolutionContext.candidates) {
mcimadamore@1215: if (currentResolutionContext.step != c.step ||
mcimadamore@1215: (c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE)) ||
mcimadamore@1215: (!c.isApplicable() && !verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))) {
mcimadamore@1215: continue;
mcimadamore@1215: } else {
mcimadamore@1215: subDiags.append(c.isApplicable() ?
mcimadamore@1215: getVerboseApplicableCandidateDiag(pos, c.sym, c.mtype) :
mcimadamore@1215: getVerboseInapplicableCandidateDiag(pos, c.sym, c.details));
mcimadamore@1215: if (c.sym == bestSoFar)
mcimadamore@1215: mostSpecificPos = pos;
mcimadamore@1215: pos++;
mcimadamore@1215: }
mcimadamore@1215: }
mcimadamore@1215: String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
mcimadamore@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) {
duke@1: return env.info.staticLevel > env.outer.info.staticLevel;
duke@1: }
duke@1:
duke@1: /** An environment is an "initializer" if it is a constructor or
duke@1: * an instance initializer.
duke@1: */
duke@1: static boolean isInitializer(Env env) {
duke@1: Symbol owner = env.info.scope.owner;
duke@1: return owner.isConstructor() ||
duke@1: owner.owner.kind == TYP &&
duke@1: (owner.kind == VAR ||
duke@1: owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
duke@1: (owner.flags() & STATIC) == 0;
duke@1: }
duke@1:
duke@1: /** Is class accessible in given evironment?
duke@1: * @param env The current environment.
duke@1: * @param c The class whose accessibility is checked.
duke@1: */
duke@1: public boolean isAccessible(Env env, TypeSymbol c) {
mcimadamore@741: return isAccessible(env, c, false);
mcimadamore@741: }
mcimadamore@741:
mcimadamore@741: public boolean isAccessible(Env env, TypeSymbol c, boolean checkInner) {
mcimadamore@741: boolean isAccessible = false;
duke@1: switch ((short)(c.flags() & AccessFlags)) {
mcimadamore@741: case PRIVATE:
mcimadamore@741: isAccessible =
mcimadamore@741: env.enclClass.sym.outermostClass() ==
mcimadamore@741: c.owner.outermostClass();
mcimadamore@741: break;
mcimadamore@741: case 0:
mcimadamore@741: isAccessible =
mcimadamore@741: env.toplevel.packge == c.owner // fast special case
mcimadamore@741: ||
mcimadamore@741: env.toplevel.packge == c.packge()
mcimadamore@741: ||
mcimadamore@741: // Hack: this case is added since synthesized default constructors
mcimadamore@741: // of anonymous classes should be allowed to access
mcimadamore@741: // classes which would be inaccessible otherwise.
mcimadamore@741: env.enclMethod != null &&
mcimadamore@741: (env.enclMethod.mods.flags & ANONCONSTR) != 0;
mcimadamore@741: break;
mcimadamore@741: default: // error recovery
mcimadamore@741: case PUBLIC:
mcimadamore@741: isAccessible = true;
mcimadamore@741: break;
mcimadamore@741: case PROTECTED:
mcimadamore@741: isAccessible =
mcimadamore@741: env.toplevel.packge == c.owner // fast special case
mcimadamore@741: ||
mcimadamore@741: env.toplevel.packge == c.packge()
mcimadamore@741: ||
mcimadamore@741: isInnerSubClass(env.enclClass.sym, c.owner);
mcimadamore@741: break;
duke@1: }
mcimadamore@741: return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
mcimadamore@741: isAccessible :
jjg@789: isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
duke@1: }
duke@1: //where
duke@1: /** Is given class a subclass of given base class, or an inner class
duke@1: * of a subclass?
duke@1: * Return null if no such class exists.
duke@1: * @param c The class which is the subclass or is contained in it.
duke@1: * @param base The base class
duke@1: */
duke@1: private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
duke@1: while (c != null && !c.isSubClass(base, types)) {
duke@1: c = c.owner.enclClass();
duke@1: }
duke@1: return c != null;
duke@1: }
duke@1:
duke@1: boolean isAccessible(Env env, Type t) {
mcimadamore@741: return isAccessible(env, t, false);
mcimadamore@741: }
mcimadamore@741:
mcimadamore@741: boolean isAccessible(Env env, Type t, boolean checkInner) {
jjg@1374: return (t.hasTag(ARRAY))
duke@1: ? isAccessible(env, types.elemtype(t))
mcimadamore@741: : isAccessible(env, t.tsym, checkInner);
duke@1: }
duke@1:
duke@1: /** Is symbol accessible as a member of given type in given evironment?
duke@1: * @param env The current environment.
duke@1: * @param site The type of which the tested symbol is regarded
duke@1: * as a member.
duke@1: * @param sym The symbol.
duke@1: */
duke@1: public boolean isAccessible(Env env, Type site, Symbol sym) {
mcimadamore@741: return isAccessible(env, site, sym, false);
mcimadamore@741: }
mcimadamore@741: public boolean isAccessible(Env env, Type site, Symbol sym, boolean checkInner) {
duke@1: if (sym.name == names.init && sym.owner != site.tsym) return false;
duke@1: switch ((short)(sym.flags() & AccessFlags)) {
duke@1: case PRIVATE:
duke@1: return
duke@1: (env.enclClass.sym == sym.owner // fast special case
duke@1: ||
duke@1: env.enclClass.sym.outermostClass() ==
duke@1: sym.owner.outermostClass())
duke@1: &&
duke@1: sym.isInheritedIn(site.tsym, types);
duke@1: case 0:
duke@1: return
duke@1: (env.toplevel.packge == sym.owner.owner // fast special case
duke@1: ||
duke@1: env.toplevel.packge == sym.packge())
duke@1: &&
mcimadamore@741: isAccessible(env, site, checkInner)
duke@1: &&
mcimadamore@254: sym.isInheritedIn(site.tsym, types)
mcimadamore@254: &&
mcimadamore@254: notOverriddenIn(site, sym);
duke@1: case PROTECTED:
duke@1: return
duke@1: (env.toplevel.packge == sym.owner.owner // fast special case
duke@1: ||
duke@1: env.toplevel.packge == sym.packge()
duke@1: ||
duke@1: isProtectedAccessible(sym, env.enclClass.sym, site)
duke@1: ||
duke@1: // OK to select instance method or field from 'super' or type name
duke@1: // (but type names should be disallowed elsewhere!)
duke@1: env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
duke@1: &&
mcimadamore@741: isAccessible(env, site, checkInner)
duke@1: &&
mcimadamore@254: notOverriddenIn(site, sym);
duke@1: default: // this case includes erroneous combinations as well
mcimadamore@741: return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
mcimadamore@254: }
mcimadamore@254: }
mcimadamore@254: //where
mcimadamore@254: /* `sym' is accessible only if not overridden by
mcimadamore@254: * another symbol which is a member of `site'
mcimadamore@254: * (because, if it is overridden, `sym' is not strictly
mcimadamore@674: * speaking a member of `site'). A polymorphic signature method
mcimadamore@674: * cannot be overridden (e.g. MH.invokeExact(Object[])).
mcimadamore@254: */
mcimadamore@254: private boolean notOverriddenIn(Type site, Symbol sym) {
mcimadamore@254: if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
mcimadamore@254: return true;
mcimadamore@254: else {
mcimadamore@254: Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
mcimadamore@844: return (s2 == null || s2 == sym || sym.owner == s2.owner ||
mcimadamore@325: !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
duke@1: }
duke@1: }
duke@1: //where
duke@1: /** Is given protected symbol accessible if it is selected from given site
duke@1: * and the selection takes place in given class?
duke@1: * @param sym The symbol with protected access
duke@1: * @param c The class where the access takes place
duke@1: * @site The type of the qualifier
duke@1: */
duke@1: private
duke@1: boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
duke@1: while (c != null &&
duke@1: !(c.isSubClass(sym.owner, types) &&
duke@1: (c.flags() & INTERFACE) == 0 &&
duke@1: // In JLS 2e 6.6.2.1, the subclass restriction applies
duke@1: // only to instance fields and methods -- types are excluded
duke@1: // regardless of whether they are declared 'static' or not.
duke@1: ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
duke@1: c = c.owner.enclClass();
duke@1: return c != null;
duke@1: }
duke@1:
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
duke@1: * given type arguments and argument types. If succesful, return
duke@1: * the method's instantiated type, else return null.
duke@1: * The instantiation will take into account an additional leading
duke@1: * formal parameter if the method is an instance method seen as a member
duke@1: * of un underdetermined site In this case, we treat site as an additional
duke@1: * parameter and the parameters of the class containing the method as
duke@1: * additional type variables that get instantiated.
duke@1: *
duke@1: * @param env The current environment
duke@1: * @param site The type of which the method is a member.
duke@1: * @param m The method symbol.
duke@1: * @param argtypes The invocation's given value arguments.
duke@1: * @param typeargtypes The invocation's given type arguments.
duke@1: * @param allowBoxing Allow boxing conversions of arguments.
duke@1: * @param useVarargs Box trailing arguments into an array for varargs.
duke@1: */
duke@1: Type rawInstantiate(Env env,
duke@1: Type site,
duke@1: Symbol m,
mcimadamore@1268: ResultInfo resultInfo,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
mcimadamore@1479: MethodCheck methodCheck,
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,
mcimadamore@580: m,
duke@1: argtypes,
duke@1: allowBoxing,
duke@1: useVarargs,
mcimadamore@1347: currentResolutionContext,
mcimadamore@1479: methodCheck,
duke@1: warn);
mcimadamore@689:
mcimadamore@1479: methodCheck.argumentsAcceptable(env, currentResolutionContext.deferredAttrContext(m, infer.emptyContext),
mcimadamore@1479: argtypes, mt.getParameterTypes(), warn);
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@1347: MethodResolutionPhase step = currentResolutionContext.step = env.info.pendingResolutionPhase;
mcimadamore@1347: return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
mcimadamore@1479: step.isBoxingRequired(), step.isVarargsRequired(), resolveMethodCheck, 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,
mcimadamore@1479: MethodCheck methodCheck,
duke@1: Warner warn) {
duke@1: try {
mcimadamore@1268: return rawInstantiate(env, site, m, resultInfo, argtypes, typeargtypes,
mcimadamore@1479: allowBoxing, useVarargs, methodCheck, 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@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@689: }
mcimadamore@1186:
mcimadamore@1186: /**
mcimadamore@1186: * Main method applicability routine. Given a list of actual types A,
mcimadamore@1186: * a list of formal types F, determines whether the types in A are
mcimadamore@1186: * compatible (by method invocation conversion) with the types in F.
mcimadamore@1186: *
mcimadamore@1186: * Since this routine is shared between overload resolution and method
mcimadamore@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@1479: MethodCheck resolveMethodCheck = new MethodCheck() {
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@1479: //should we expand formals?
mcimadamore@1479: boolean useVarargs = deferredAttrContext.phase.isVarargsRequired();
mcimadamore@1479:
mcimadamore@1479: //inference context used during this method check
mcimadamore@1479: InferenceContext inferenceContext = deferredAttrContext.inferenceContext;
mcimadamore@1479:
mcimadamore@1479: Type varargsFormal = useVarargs ? formals.last() : null;
mcimadamore@1479:
mcimadamore@1479: if (varargsFormal == null &&
mcimadamore@1479: argtypes.size() != formals.size()) {
mcimadamore@1479: report(MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
mcimadamore@1479: }
mcimadamore@1479:
mcimadamore@1479: while (argtypes.nonEmpty() && formals.head != varargsFormal) {
mcimadamore@1479: ResultInfo mresult = methodCheckResult(false, formals.head, deferredAttrContext, warn);
mcimadamore@1347: mresult.check(null, argtypes.head);
mcimadamore@689: argtypes = argtypes.tail;
mcimadamore@1479: formals = formals.tail;
mcimadamore@689: }
mcimadamore@1479:
mcimadamore@1479: if (formals.head != varargsFormal) {
mcimadamore@1479: report(MethodCheckDiag.ARITY_MISMATCH, inferenceContext); // not enough args
mcimadamore@1479: }
mcimadamore@1479:
mcimadamore@1479: if (useVarargs) {
mcimadamore@1479: //note: if applicability check is triggered by most specific test,
mcimadamore@1479: //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
mcimadamore@1479: final Type elt = types.elemtype(varargsFormal);
mcimadamore@1479: ResultInfo mresult = methodCheckResult(true, elt, deferredAttrContext, warn);
mcimadamore@1479: while (argtypes.nonEmpty()) {
mcimadamore@1479: mresult.check(null, argtypes.head);
mcimadamore@1479: argtypes = argtypes.tail;
mcimadamore@1337: }
mcimadamore@1479: //check varargs element type accessibility
mcimadamore@1479: varargsAccessible(env, elt, inferenceContext);
mcimadamore@845: }
duke@1: }
mcimadamore@1479:
mcimadamore@1479: private void report(MethodCheckDiag diag, InferenceContext inferenceContext, Object... args) {
mcimadamore@1479: boolean inferDiag = inferenceContext != infer.emptyContext;
mcimadamore@1479: InapplicableMethodException ex = inferDiag ?
mcimadamore@1479: infer.inferenceException : inapplicableMethodException;
mcimadamore@1479: if (inferDiag && (!diag.inferKey.equals(diag.basicKey))) {
mcimadamore@1479: Object[] args2 = new Object[args.length + 1];
mcimadamore@1479: System.arraycopy(args, 0, args2, 1, args.length);
mcimadamore@1479: args2[0] = inferenceContext.inferenceVars();
mcimadamore@1479: args = args2;
mcimadamore@1479: }
mcimadamore@1479: throw ex.setMessage(inferDiag ? diag.inferKey : diag.basicKey, args);
mcimadamore@1479: }
mcimadamore@1479:
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@1479: varargsAccessible(env, inferenceContext.asInstType(t, types), inferenceContext);
mcimadamore@1479: }
mcimadamore@1479: });
mcimadamore@1479: } else {
mcimadamore@1479: if (!isAccessible(env, t)) {
mcimadamore@1479: Symbol location = env.enclClass.sym;
mcimadamore@1479: report(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@1479: report(methodDiag, deferredAttrContext.inferenceContext, details);
mcimadamore@1479: }
mcimadamore@1479: };
mcimadamore@1479: return new MethodResultInfo(to, checkContext);
mcimadamore@1479: }
mcimadamore@1479: };
mcimadamore@689:
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) {
mcimadamore@1479: return strict ?
mcimadamore@1479: types.isSubtypeUnchecked(found, deferredAttrContext.inferenceContext.asFree(req, types), warn) :
mcimadamore@1479: types.isConvertible(found, deferredAttrContext.inferenceContext.asFree(req, types), 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: }
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 {
mcimadamore@1338: return super.check(pos, chk.checkNonVoid(pos, types.capture(types.upperBound(found.baseType()))));
mcimadamore@689: }
mcimadamore@1347: }
mcimadamore@1347:
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) {
mcimadamore@1510: if (!allowStructuralMostSpecific || actual == null) {
mcimadamore@1510: return super.compatible(found, req, warn);
mcimadamore@1510: } else {
mcimadamore@1510: switch (actual.getTag()) {
mcimadamore@1510: case DEFERRED:
mcimadamore@1510: DeferredType dt = (DeferredType) actual;
mcimadamore@1510: DeferredType.SpeculativeCache.Entry e = dt.speculativeCache.get(deferredAttrContext.msym, deferredAttrContext.phase);
mcimadamore@1510: return (e == null || e.speculativeTree == deferredAttr.stuckTree)
mcimadamore@1510: ? false : mostSpecific(found, req, e.speculativeTree, warn);
mcimadamore@1510: default:
mcimadamore@1510: return standaloneMostSpecific(found, req, actual, warn);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: private boolean mostSpecific(Type t, Type s, JCTree tree, Warner warn) {
mcimadamore@1510: MostSpecificChecker msc = new MostSpecificChecker(t, s, warn);
mcimadamore@1510: msc.scan(tree);
mcimadamore@1510: return msc.result;
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: boolean polyMostSpecific(Type t1, Type t2, Warner warn) {
mcimadamore@1510: return (!t1.isPrimitive() && t2.isPrimitive())
mcimadamore@1510: ? true : super.compatible(t1, t2, warn);
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: boolean standaloneMostSpecific(Type t1, Type t2, Type exprType, Warner warn) {
mcimadamore@1510: return (exprType.isPrimitive() == t1.isPrimitive()
mcimadamore@1510: && exprType.isPrimitive() != t2.isPrimitive())
mcimadamore@1510: ? true : super.compatible(t1, t2, warn);
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: /**
mcimadamore@1510: * Structural checker for most specific.
mcimadamore@1510: */
mcimadamore@1510: class MostSpecificChecker 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:
mcimadamore@1510: MostSpecificChecker(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) {
mcimadamore@1510: result &= standaloneMostSpecific(t, s, tree.type, warn);
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
mcimadamore@1510: public void visitConditional(JCConditional tree) {
mcimadamore@1510: if (tree.polyKind == PolyKind.STANDALONE) {
mcimadamore@1510: result &= standaloneMostSpecific(t, s, tree.type, warn);
mcimadamore@1510: } else {
mcimadamore@1510: super.visitConditional(tree);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
mcimadamore@1510: public void visitApply(JCMethodInvocation tree) {
mcimadamore@1510: result &= (tree.polyKind == PolyKind.STANDALONE)
mcimadamore@1510: ? standaloneMostSpecific(t, s, tree.type, warn)
mcimadamore@1510: : polyMostSpecific(t, s, warn);
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
mcimadamore@1510: public void visitNewClass(JCNewClass tree) {
mcimadamore@1510: result &= (tree.polyKind == PolyKind.STANDALONE)
mcimadamore@1510: ? standaloneMostSpecific(t, s, tree.type, warn)
mcimadamore@1510: : polyMostSpecific(t, s, warn);
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
mcimadamore@1510: public void visitReference(JCMemberReference tree) {
mcimadamore@1510: if (types.isFunctionalInterface(t.tsym) &&
mcimadamore@1510: types.isFunctionalInterface(s.tsym) &&
mcimadamore@1510: types.asSuper(t, s.tsym) == null &&
mcimadamore@1510: types.asSuper(s, t.tsym) == null) {
mcimadamore@1510: Type desc_t = types.findDescriptorType(t);
mcimadamore@1510: Type desc_s = types.findDescriptorType(s);
mcimadamore@1510: if (types.isSameTypes(desc_t.getParameterTypes(), desc_s.getParameterTypes())) {
mcimadamore@1510: if (!desc_s.getReturnType().hasTag(VOID)) {
mcimadamore@1510: //perform structural comparison
mcimadamore@1510: Type ret_t = desc_t.getReturnType();
mcimadamore@1510: Type ret_s = desc_s.getReturnType();
mcimadamore@1510: result &= ((tree.refPolyKind == PolyKind.STANDALONE)
mcimadamore@1510: ? standaloneMostSpecific(ret_t, ret_s, tree.type, warn)
mcimadamore@1510: : polyMostSpecific(ret_t, ret_s, warn));
mcimadamore@1510: } else {
mcimadamore@1510: return;
mcimadamore@1510: }
mcimadamore@1510: } else {
mcimadamore@1510: result &= false;
mcimadamore@1510: }
mcimadamore@1510: } else {
mcimadamore@1510: result &= MostSpecificCheckContext.super.compatible(t, s, warn);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510:
mcimadamore@1510: @Override
mcimadamore@1510: public void visitLambda(JCLambda tree) {
mcimadamore@1510: if (types.isFunctionalInterface(t.tsym) &&
mcimadamore@1510: types.isFunctionalInterface(s.tsym) &&
mcimadamore@1510: types.asSuper(t, s.tsym) == null &&
mcimadamore@1510: types.asSuper(s, t.tsym) == null) {
mcimadamore@1510: Type desc_t = types.findDescriptorType(t);
mcimadamore@1510: Type desc_s = types.findDescriptorType(s);
mcimadamore@1510: if (tree.paramKind == JCLambda.ParameterKind.EXPLICIT
mcimadamore@1510: || types.isSameTypes(desc_t.getParameterTypes(), desc_s.getParameterTypes())) {
mcimadamore@1510: if (!desc_s.getReturnType().hasTag(VOID)) {
mcimadamore@1510: //perform structural comparison
mcimadamore@1510: Type ret_t = desc_t.getReturnType();
mcimadamore@1510: Type ret_s = desc_s.getReturnType();
mcimadamore@1510: scanLambdaBody(tree, ret_t, ret_s);
mcimadamore@1510: } else {
mcimadamore@1510: return;
mcimadamore@1510: }
mcimadamore@1510: } else {
mcimadamore@1510: result &= false;
mcimadamore@1510: }
mcimadamore@1510: } else {
mcimadamore@1510: result &= MostSpecificCheckContext.super.compatible(t, s, warn);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: //where
mcimadamore@1510:
mcimadamore@1510: void scanLambdaBody(JCLambda lambda, final Type t, final Type s) {
mcimadamore@1510: if (lambda.getBodyKind() == JCTree.JCLambda.BodyKind.EXPRESSION) {
mcimadamore@1510: result &= MostSpecificCheckContext.this.mostSpecific(t, s, lambda.body, warn);
mcimadamore@1510: } else {
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) {
mcimadamore@1510: result &= MostSpecificCheckContext.this.mostSpecific(t, s, tree.expr, warn);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: };
mcimadamore@1510: lambdaScanner.scan(lambda.body);
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: }
mcimadamore@1510: }
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);
duke@1: if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1: sym.owner != bestSoFar.owner)
duke@1: bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1: else if (sym.kind < bestSoFar.kind)
duke@1: bestSoFar = sym;
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Resolve a field identifier, throw a fatal error if not found.
duke@1: * @param pos The position to use for error reporting.
duke@1: * @param env The environment current at the method invocation.
duke@1: * @param site The type of the qualifying expression, in which
duke@1: * identifier is searched.
duke@1: * @param name The identifier's name.
duke@1: */
duke@1: public VarSymbol resolveInternalField(DiagnosticPosition pos, Env env,
duke@1: Type site, Name name) {
duke@1: Symbol sym = findField(env, site, name, site.tsym);
duke@1: if (sym.kind == VAR) return (VarSymbol)sym;
duke@1: else throw new FatalError(
mcimadamore@89: diags.fragment("fatal.err.cant.locate.field",
duke@1: name));
duke@1: }
duke@1:
duke@1: /** Find unqualified variable or field with given name.
duke@1: * Synthetic fields always skipped.
duke@1: * @param env The current environment.
duke@1: * @param name The name of the variable or field.
duke@1: */
duke@1: Symbol findVar(Env env, Name name) {
duke@1: Symbol bestSoFar = varNotFound;
duke@1: Symbol sym;
duke@1: Env env1 = env;
duke@1: boolean staticOnly = false;
duke@1: while (env1.outer != null) {
duke@1: if (isStatic(env1)) staticOnly = true;
duke@1: Scope.Entry e = env1.info.scope.lookup(name);
duke@1: while (e.scope != null &&
duke@1: (e.sym.kind != VAR ||
duke@1: (e.sym.flags_field & SYNTHETIC) != 0))
duke@1: e = e.next();
duke@1: sym = (e.scope != null)
duke@1: ? e.sym
duke@1: : findField(
duke@1: env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
duke@1: if (sym.exists()) {
duke@1: if (staticOnly &&
duke@1: sym.kind == VAR &&
duke@1: sym.owner.kind == TYP &&
duke@1: (sym.flags() & STATIC) == 0)
duke@1: return new StaticError(sym);
duke@1: else
duke@1: return sym;
duke@1: } else if (sym.kind < bestSoFar.kind) {
duke@1: bestSoFar = sym;
duke@1: }
duke@1:
duke@1: if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1: env1 = env1.outer;
duke@1: }
duke@1:
duke@1: sym = findField(env, syms.predefClass.type, name, syms.predefClass);
duke@1: if (sym.exists())
duke@1: return sym;
duke@1: if (bestSoFar.exists())
duke@1: return bestSoFar;
duke@1:
duke@1: Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1: for (; e.scope != null; e = e.next()) {
duke@1: sym = e.sym;
duke@1: Type origin = e.getOrigin().owner.type;
duke@1: if (sym.kind == VAR) {
duke@1: if (e.sym.owner.type != origin)
duke@1: sym = sym.clone(e.getOrigin().owner);
duke@1: return isAccessible(env, origin, sym)
duke@1: ? sym : new AccessError(env, origin, sym);
duke@1: }
duke@1: }
duke@1:
duke@1: Symbol origin = null;
duke@1: e = env.toplevel.starImportScope.lookup(name);
duke@1: for (; e.scope != null; e = e.next()) {
duke@1: sym = e.sym;
duke@1: if (sym.kind != VAR)
duke@1: continue;
duke@1: // invariant: sym.kind == VAR
duke@1: if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
duke@1: return new AmbiguityError(bestSoFar, sym);
duke@1: else if (bestSoFar.kind >= VAR) {
duke@1: origin = e.getOrigin().owner;
duke@1: bestSoFar = isAccessible(env, origin.type, sym)
duke@1: ? sym : new AccessError(env, origin.type, sym);
duke@1: }
duke@1: }
duke@1: if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
duke@1: return bestSoFar.clone(origin);
duke@1: else
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: Warner noteWarner = new Warner();
duke@1:
duke@1: /** Select the best method for a call site among two choices.
duke@1: * @param env The current environment.
duke@1: * @param site The original type from where the
duke@1: * selection takes place.
duke@1: * @param argtypes The invocation's value arguments,
duke@1: * @param typeargtypes The invocation's type arguments,
duke@1: * @param sym Proposed new best match.
duke@1: * @param bestSoFar Previously found best match.
duke@1: * @param allowBoxing Allow boxing conversions of arguments.
duke@1: * @param useVarargs Box trailing arguments into an array for varargs.
duke@1: */
mcimadamore@689: @SuppressWarnings("fallthrough")
duke@1: Symbol selectBest(Env env,
duke@1: Type site,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: Symbol sym,
duke@1: Symbol bestSoFar,
duke@1: boolean allowBoxing,
duke@1: boolean useVarargs,
duke@1: boolean operator) {
mcimadamore@1394: if (sym.kind == ERR ||
mcimadamore@1394: !sym.isInheritedIn(site.tsym, types) ||
mcimadamore@1394: (useVarargs && (sym.flags() & VARARGS) == 0)) {
mcimadamore@1394: return 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@1479: allowBoxing, useVarargs, resolveMethodCheck, types.noWarnings);
mcimadamore@1215: if (!operator)
mcimadamore@1215: currentResolutionContext.addApplicableCandidate(sym, mt);
mcimadamore@689: } catch (InapplicableMethodException ex) {
mcimadamore@1215: if (!operator)
mcimadamore@1215: currentResolutionContext.addInapplicableCandidate(sym, ex.getDiagnostic());
duke@1: switch (bestSoFar.kind) {
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:
mcimadamore@1480: //check if m1 is more specific than all ambiguous methods in m2
duke@1: AmbiguityError e = (AmbiguityError)m2;
mcimadamore@1480: for (Symbol s : e.ambiguousSyms) {
mcimadamore@1480: if (mostSpecific(argtypes, m1, s, env, site, allowBoxing, useVarargs) != m1) {
mcimadamore@1480: return e.addAmbiguousSymbol(m1);
mcimadamore@1480: }
mcimadamore@1480: }
mcimadamore@1480: return m1;
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@1510: Type mst = instantiate(env, site, m2, null,
mcimadamore@1510: adjustArgs(types.lowerBounds(types.memberType(site, m1).getParameterTypes()), m1, maxLength, useVarargs), null,
mcimadamore@1510: allowBoxing, useVarargs, new MostSpecificCheck(!allowBoxing, actuals), noteWarner);
mcimadamore@1510: return mst != null &&
mcimadamore@1510: !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
mcimadamore@1510: }
mcimadamore@1510: private 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: reportVerboseResolutionDiagnostic(env.tree.pos(), name, site, argtypes, typeargtypes, bestSoFar);
mcimadamore@1114: return bestSoFar;
duke@1: }
duke@1: // where
duke@1: private Symbol findMethod(Env env,
duke@1: Type site,
duke@1: Name name,
duke@1: List argtypes,
duke@1: List typeargtypes,
duke@1: Type intype,
duke@1: 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@1393: if (iphase2 == InterfaceLookupPhase.DEFAULT_OK && !allowDefaultMethods) break;
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;
mcimadamore@1393: } else if (rs.allowDefaultMethods) {
mcimadamore@1393: return DEFAULT_OK;
mcimadamore@1393: } else {
mcimadamore@1393: return null;
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;
duke@1: sym = findMethod(
duke@1: env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
duke@1: allowBoxing, useVarargs, false);
duke@1: if (sym.exists()) {
duke@1: if (staticOnly &&
duke@1: sym.kind == MTH &&
duke@1: sym.owner.kind == TYP &&
duke@1: (sym.flags() & STATIC) == 0) return new StaticError(sym);
duke@1: else return sym;
duke@1: } else if (sym.kind < bestSoFar.kind) {
duke@1: bestSoFar = sym;
duke@1: }
duke@1: if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1: env1 = env1.outer;
duke@1: }
duke@1:
duke@1: sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1: typeargtypes, allowBoxing, useVarargs, false);
duke@1: if (sym.exists())
duke@1: return sym;
duke@1:
duke@1: Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1: for (; e.scope != null; e = e.next()) {
duke@1: sym = e.sym;
duke@1: Type origin = e.getOrigin().owner.type;
duke@1: if (sym.kind == MTH) {
duke@1: if (e.sym.owner.type != origin)
duke@1: sym = sym.clone(e.getOrigin().owner);
duke@1: if (!isAccessible(env, origin, sym))
duke@1: sym = new AccessError(env, origin, sym);
duke@1: bestSoFar = selectBest(env, origin,
duke@1: argtypes, typeargtypes,
duke@1: sym, bestSoFar,
duke@1: allowBoxing, useVarargs, false);
duke@1: }
duke@1: }
duke@1: if (bestSoFar.exists())
duke@1: return bestSoFar;
duke@1:
duke@1: e = env.toplevel.starImportScope.lookup(name);
duke@1: for (; e.scope != null; e = e.next()) {
duke@1: sym = e.sym;
duke@1: Type origin = e.getOrigin().owner.type;
duke@1: if (sym.kind == MTH) {
duke@1: if (e.sym.owner.type != origin)
duke@1: sym = sym.clone(e.getOrigin().owner);
duke@1: if (!isAccessible(env, origin, sym))
duke@1: sym = new AccessError(env, origin, sym);
duke@1: bestSoFar = selectBest(env, origin,
duke@1: argtypes, typeargtypes,
duke@1: sym, bestSoFar,
duke@1: allowBoxing, useVarargs, false);
duke@1: }
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Load toplevel or member class with given fully qualified name and
duke@1: * verify that it is accessible.
duke@1: * @param env The current environment.
duke@1: * @param name The fully qualified name of the class to be loaded.
duke@1: */
duke@1: Symbol loadClass(Env env, Name name) {
duke@1: try {
duke@1: ClassSymbol c = reader.loadClass(name);
duke@1: return isAccessible(env, c) ? c : new AccessError(c);
duke@1: } catch (ClassReader.BadClassFile err) {
duke@1: throw err;
duke@1: } catch (CompletionFailure ex) {
duke@1: return typeNotFound;
duke@1: }
duke@1: }
duke@1:
duke@1: /** Find qualified member type.
duke@1: * @param env The current environment.
duke@1: * @param site The original type from where the selection takes
duke@1: * place.
duke@1: * @param name The type's name.
duke@1: * @param c The class to search for the member type. This is
duke@1: * always a superclass or implemented interface of
duke@1: * site's class.
duke@1: */
duke@1: Symbol findMemberType(Env env,
duke@1: Type site,
duke@1: Name name,
duke@1: TypeSymbol c) {
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: Symbol sym;
duke@1: Scope.Entry e = c.members().lookup(name);
duke@1: while (e.scope != null) {
duke@1: if (e.sym.kind == TYP) {
duke@1: return isAccessible(env, site, e.sym)
duke@1: ? e.sym
duke@1: : new AccessError(env, site, e.sym);
duke@1: }
duke@1: e = e.next();
duke@1: }
duke@1: Type st = types.supertype(c.type);
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:
duke@1: /** Find a global type in given scope and load corresponding class.
duke@1: * @param env The current environment.
duke@1: * @param scope The scope in which to look for the type.
duke@1: * @param name The type's name.
duke@1: */
duke@1: Symbol findGlobalType(Env env, Scope scope, Name name) {
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
duke@1: Symbol sym = loadClass(env, e.sym.flatName());
duke@1: if (bestSoFar.kind == TYP && sym.kind == TYP &&
duke@1: bestSoFar != sym)
duke@1: return new AmbiguityError(bestSoFar, sym);
duke@1: else if (sym.kind < bestSoFar.kind)
duke@1: bestSoFar = sym;
duke@1: }
duke@1: return bestSoFar;
duke@1: }
duke@1:
duke@1: /** Find an unqualified type symbol.
duke@1: * @param env The current environment.
duke@1: * @param name The type's name.
duke@1: */
duke@1: Symbol findType(Env env, Name name) {
duke@1: Symbol bestSoFar = typeNotFound;
duke@1: Symbol sym;
duke@1: boolean staticOnly = false;
duke@1: for (Env env1 = env; env1.outer != null; env1 = env1.outer) {
duke@1: if (isStatic(env1)) staticOnly = true;
duke@1: for (Scope.Entry e = env1.info.scope.lookup(name);
duke@1: e.scope != null;
duke@1: e = e.next()) {
duke@1: if (e.sym.kind == TYP) {
duke@1: if (staticOnly &&
jjg@1374: e.sym.type.hasTag(TYPEVAR) &&
duke@1: e.sym.owner.kind == TYP) return new StaticError(e.sym);
duke@1: return e.sym;
duke@1: }
duke@1: }
duke@1:
duke@1: sym = findMemberType(env1, env1.enclClass.sym.type, name,
duke@1: env1.enclClass.sym);
duke@1: if (staticOnly && sym.kind == TYP &&
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) {
mcimadamore@1347: ResolveError errSym = (ResolveError)sym;
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 :
mcimadamore@1415: Type.map(argtypes, new ResolveDeferredRecoveryMap(accessedSym));
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: class ResolveDeferredRecoveryMap extends DeferredAttr.RecoveryDeferredTypeMap {
mcimadamore@1415:
mcimadamore@1415: public ResolveDeferredRecoveryMap(Symbol msym) {
mcimadamore@1415: deferredAttr.super(AttrMode.SPECULATIVE, msym, currentResolutionContext.step);
mcimadamore@1415: }
mcimadamore@1415:
mcimadamore@1415: @Override
mcimadamore@1415: protected Type typeOf(DeferredType dt) {
mcimadamore@1415: Type res = super.typeOf(dt);
mcimadamore@1415: if (!res.isErroneous()) {
mcimadamore@1415: switch (TreeInfo.skipParens(dt.tree).getTag()) {
mcimadamore@1415: case LAMBDA:
mcimadamore@1415: case REFERENCE:
mcimadamore@1415: return dt;
mcimadamore@1415: case CONDEXPR:
mcimadamore@1415: return res == Type.recoveryType ?
mcimadamore@1415: dt : res;
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1415: return res;
mcimadamore@1347: }
mcimadamore@1347: }
mcimadamore@1347: };
mcimadamore@1347:
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@1396: return lookupMethod(env, pos, env.enclClass.sym, new BasicLookupHelper(name, env.enclClass.sym.type, argtypes, typeargtypes) {
mcimadamore@1394: @Override
mcimadamore@1394: Symbol lookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1394: return findFun(env, name, argtypes, typeargtypes,
mcimadamore@1394: phase.isBoxingRequired(),
mcimadamore@1394: phase.isVarargsRequired());
mcimadamore@1215: }
mcimadamore@1394: });
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@1394: Symbol lookup(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;
mcimadamore@1394: if (msym.isSignaturePolymorphic(types)) {
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@1239: polymorphicSignatureScope.enter(msym);
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@1394: Symbol lookup(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@1394: return lookupMethod(env, pos, site.tsym, new BasicLookupHelper(names.init, site, argtypes, typeargtypes) {
mcimadamore@1394: @Override
mcimadamore@1394: Symbol lookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1394: return findDiamond(env, site, argtypes, typeargtypes,
mcimadamore@1394: phase.isBoxingRequired(),
mcimadamore@1394: phase.isVarargsRequired());
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: final JCDiagnostic details = sym.kind == WRONG_MTH ?
mcimadamore@1394: ((InapplicableSymbolError)sym).errCandidate().details :
mcimadamore@1394: null;
mcimadamore@1394: sym = new InapplicableSymbolError(sym.kind, "diamondError", currentResolutionContext) {
mcimadamore@1394: @Override
mcimadamore@1394: JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
mcimadamore@1394: Symbol location, Type site, Name name, List argtypes, List typeargtypes) {
mcimadamore@1394: String key = details == null ?
mcimadamore@1394: "cant.apply.diamond" :
mcimadamore@1394: "cant.apply.diamond.1";
mcimadamore@1394: return diags.create(dkind, log.currentSource(), pos, key,
mcimadamore@1394: diags.fragment("diamond", site.tsym), details);
mcimadamore@1394: }
mcimadamore@1394: };
mcimadamore@1394: sym = accessMethod(sym, pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@1394: env.info.pendingResolutionPhase = currentResolutionContext.step;
mcimadamore@1394: }
mcimadamore@1394: return sym;
mcimadamore@1215: }
mcimadamore@1394: });
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@1479: env.info.pendingResolutionPhase = currentResolutionContext.step = BASIC;
mcimadamore@1394: Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
mcimadamore@1394: null, false, false, true);
mcimadamore@1394: if (boxingEnabled && sym.kind >= WRONG_MTHS)
mcimadamore@1479: env.info.pendingResolutionPhase = currentResolutionContext.step = BOX;
mcimadamore@1394: sym = findMethod(env, syms.predefClass.type, name, argtypes,
mcimadamore@1394: null, true, false, true);
mcimadamore@1394: return accessMethod(sym, pos, env.enclClass.sym.type, name,
mcimadamore@1394: false, argtypes, null);
mcimadamore@1394: }
mcimadamore@1394: 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:
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: */
mcimadamore@1352: Pair resolveMemberReference(DiagnosticPosition pos,
mcimadamore@1352: Env env,
mcimadamore@1352: JCMemberReference referenceTree,
mcimadamore@1352: Type site,
mcimadamore@1352: Name name, List argtypes,
mcimadamore@1352: List typeargtypes,
mcimadamore@1352: boolean boxingAllowed) {
mcimadamore@1394: MethodResolutionPhase maxPhase = boxingAllowed ? VARARITY : BASIC;
mcimadamore@1496:
mcimadamore@1496: ReferenceLookupHelper boundLookupHelper;
mcimadamore@1496: if (!name.equals(names.init)) {
mcimadamore@1496: //method reference
mcimadamore@1496: boundLookupHelper =
mcimadamore@1496: new MethodReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1496: } else if (site.hasTag(ARRAY)) {
mcimadamore@1496: //array constructor reference
mcimadamore@1496: boundLookupHelper =
mcimadamore@1496: new ArrayConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1496: } else {
mcimadamore@1496: //class constructor reference
mcimadamore@1496: boundLookupHelper =
mcimadamore@1496: new ConstructorReferenceLookupHelper(referenceTree, site, argtypes, typeargtypes, maxPhase);
mcimadamore@1496: }
mcimadamore@1496:
mcimadamore@1352: //step 1 - bound lookup
mcimadamore@1352: Env boundEnv = env.dup(env.tree, env.info.dup());
mcimadamore@1394: Symbol boundSym = lookupMethod(boundEnv, env.tree.pos(), site.tsym, boundLookupHelper);
mcimadamore@1352:
mcimadamore@1352: //step 2 - unbound lookup
mcimadamore@1352: ReferenceLookupHelper unboundLookupHelper = boundLookupHelper.unboundLookup();
mcimadamore@1352: Env unboundEnv = env.dup(env.tree, env.info.dup());
mcimadamore@1394: Symbol unboundSym = lookupMethod(unboundEnv, env.tree.pos(), site.tsym, unboundLookupHelper);
mcimadamore@1352:
mcimadamore@1352: //merge results
mcimadamore@1352: Pair res;
mcimadamore@1352: if (unboundSym.kind != MTH) {
mcimadamore@1352: res = new Pair(boundSym, boundLookupHelper);
mcimadamore@1352: env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase;
mcimadamore@1352: } else if (boundSym.kind == MTH) {
mcimadamore@1352: res = new Pair(ambiguityError(boundSym, unboundSym), boundLookupHelper);
mcimadamore@1352: env.info.pendingResolutionPhase = boundEnv.info.pendingResolutionPhase;
mcimadamore@1352: } else {
mcimadamore@1352: res = new Pair(unboundSym, unboundLookupHelper);
mcimadamore@1352: env.info.pendingResolutionPhase = unboundEnv.info.pendingResolutionPhase;
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: return res;
mcimadamore@1352: }
mcimadamore@1352:
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@1394: protected 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@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@1394: super(name, site, argtypes, typeargtypes, MethodResolutionPhase.VARARITY);
mcimadamore@1394: }
mcimadamore@1394:
mcimadamore@1394: @Override
mcimadamore@1394: Symbol access(Env env, DiagnosticPosition pos, Symbol location, Symbol sym) {
mcimadamore@1480: if (sym.kind == AMBIGUOUS) {
mcimadamore@1480: AmbiguityError a_err = (AmbiguityError)sym;
mcimadamore@1480: sym = a_err.mergeAbstracts(site);
mcimadamore@1480: }
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@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@1394:
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@1352: ReferenceLookupHelper unboundLookup() {
mcimadamore@1352: //dummy loopkup helper that always return 'methodNotFound'
mcimadamore@1394: return new ReferenceLookupHelper(referenceTree, name, site, argtypes, typeargtypes, maxPhase) {
mcimadamore@1352: @Override
mcimadamore@1352: ReferenceLookupHelper unboundLookup() {
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) {
mcimadamore@1480: AmbiguityError a_err = (AmbiguityError)sym;
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@1352: protected Symbol lookupReferenceInternal(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: protected Symbol adjustLookupResult(Env env, Symbol sym) {
mcimadamore@1352: return !TreeInfo.isStaticSelector(referenceTree.expr, names) ||
mcimadamore@1352: sym.kind != MTH ||
mcimadamore@1352: sym.isStatic() ? sym : new StaticError(sym);
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1394: final Symbol lookup(Env env, MethodResolutionPhase phase) {
mcimadamore@1352: return adjustLookupResult(env, lookupReferenceInternal(env, phase));
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1352: ReferenceLookupHelper unboundLookup() {
mcimadamore@1352: if (TreeInfo.isStaticSelector(referenceTree.expr, names) &&
mcimadamore@1352: argtypes.nonEmpty() &&
mcimadamore@1352: types.isSubtypeUnchecked(argtypes.head, site)) {
mcimadamore@1352: return new UnboundMethodReferenceLookupHelper(referenceTree, name,
mcimadamore@1394: site, argtypes, typeargtypes, maxPhase);
mcimadamore@1352: } else {
mcimadamore@1352: return super.unboundLookup();
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@1352: super(referenceTree, name,
mcimadamore@1352: site.isRaw() ? types.asSuper(argtypes.head, site.tsym) : site,
mcimadamore@1394: argtypes.tail, typeargtypes, maxPhase);
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1352: protected Symbol adjustLookupResult(Env env, Symbol sym) {
mcimadamore@1352: return sym.kind != MTH || !sym.isStatic() ? sym : new StaticError(sym);
mcimadamore@1352: }
mcimadamore@1352:
mcimadamore@1352: @Override
mcimadamore@1352: ReferenceLookupHelper unboundLookup() {
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);
mcimadamore@1496: 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@1394: Symbol lookupMethod(Env env, DiagnosticPosition pos, Symbol location, LookupHelper lookupHelper) {
mcimadamore@1394: return lookupMethod(env, pos, location, new MethodResolutionContext(), 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@1394: bestSoFar = phase.mergeResults(bestSoFar, lookupHelper.lookup(env, phase));
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: }
mcimadamore@1393: if (allowDefaultMethods && c.isInterface() &&
mcimadamore@1393: name == names._super && !isStatic(env) &&
mcimadamore@1415: 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) {
mcimadamore@1393: ListBuffer result = ListBuffer.lb();
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 typeargtypes) {
mcimadamore@302: JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
mcimadamore@829: pos, location, site, name, argtypes, typeargtypes);
jjg@643: if (d != null) {
jjg@643: d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
mcimadamore@302: log.report(d);
jjg@643: }
duke@1: }
duke@1:
mcimadamore@161: private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
mcimadamore@161:
mcimadamore@161: public Object methodArguments(List argtypes) {
mcimadamore@1348: if (argtypes == null || argtypes.isEmpty()) {
mcimadamore@1348: return noArgs;
mcimadamore@1348: } else {
mcimadamore@1348: ListBuffer