src/share/classes/com/sun/tools/javac/comp/Resolve.java

Tue, 24 Jan 2012 17:52:02 +0000

author
mcimadamore
date
Tue, 24 Jan 2012 17:52:02 +0000
changeset 1186
51fb17abfc32
parent 1127
ca49d50318dc
child 1215
161230ec7c73
permissions
-rw-r--r--

7129801: Merge the two method applicability routines
Summary: Resolve.java and Infer.java should reuse the same method applicability check routine
Reviewed-by: dlsmith, jjg

duke@1 1 /*
mcimadamore@1186 2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
mcimadamore@1114 28 import com.sun.tools.javac.api.Formattable.LocalizedString;
duke@1 29 import com.sun.tools.javac.code.*;
mcimadamore@1114 30 import com.sun.tools.javac.code.Type.*;
mcimadamore@1114 31 import com.sun.tools.javac.code.Symbol.*;
duke@1 32 import com.sun.tools.javac.jvm.*;
duke@1 33 import com.sun.tools.javac.tree.*;
mcimadamore@1114 34 import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@1114 35 import com.sun.tools.javac.util.*;
mcimadamore@1114 36 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticFlag;
mcimadamore@1114 37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
mcimadamore@1114 38 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
duke@1 39
mcimadamore@1114 40 import java.util.Arrays;
mcimadamore@1114 41 import java.util.Collection;
mcimadamore@1114 42 import java.util.EnumSet;
mcimadamore@1114 43 import java.util.HashMap;
mcimadamore@1114 44 import java.util.HashSet;
mcimadamore@1114 45 import java.util.LinkedHashMap;
mcimadamore@1114 46 import java.util.Map;
mcimadamore@1114 47 import java.util.Set;
mcimadamore@1114 48
mcimadamore@1114 49 import javax.lang.model.element.ElementVisitor;
duke@1 50
duke@1 51 import static com.sun.tools.javac.code.Flags.*;
jjg@1127 52 import static com.sun.tools.javac.code.Flags.BLOCK;
duke@1 53 import static com.sun.tools.javac.code.Kinds.*;
jjg@1127 54 import static com.sun.tools.javac.code.Kinds.ERRONEOUS;
duke@1 55 import static com.sun.tools.javac.code.TypeTags.*;
mcimadamore@1114 56 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
jjg@1127 57 import static com.sun.tools.javac.tree.JCTree.Tag.*;
mcimadamore@160 58
duke@1 59 /** Helper class for name resolution, used mostly by the attribution phase.
duke@1 60 *
jjg@581 61 * <p><b>This is NOT part of any supported API.
jjg@581 62 * If you write code that depends on this, you do so at your own risk.
duke@1 63 * This code and its internal interfaces are subject to change or
duke@1 64 * deletion without notice.</b>
duke@1 65 */
duke@1 66 public class Resolve {
duke@1 67 protected static final Context.Key<Resolve> resolveKey =
duke@1 68 new Context.Key<Resolve>();
duke@1 69
jjg@113 70 Names names;
duke@1 71 Log log;
duke@1 72 Symtab syms;
duke@1 73 Check chk;
duke@1 74 Infer infer;
duke@1 75 ClassReader reader;
duke@1 76 TreeInfo treeinfo;
duke@1 77 Types types;
mcimadamore@89 78 JCDiagnostic.Factory diags;
duke@1 79 public final boolean boxingEnabled; // = source.allowBoxing();
duke@1 80 public final boolean varargsEnabled; // = source.allowVarargs();
mcimadamore@674 81 public final boolean allowMethodHandles;
duke@1 82 private final boolean debugResolve;
mcimadamore@1114 83 final EnumSet<VerboseResolutionMode> verboseResolutionMode;
duke@1 84
mcimadamore@674 85 Scope polymorphicSignatureScope;
mcimadamore@674 86
mcimadamore@1114 87 enum VerboseResolutionMode {
mcimadamore@1114 88 SUCCESS("success"),
mcimadamore@1114 89 FAILURE("failure"),
mcimadamore@1114 90 APPLICABLE("applicable"),
mcimadamore@1114 91 INAPPLICABLE("inapplicable"),
mcimadamore@1114 92 DEFERRED_INST("deferred-inference"),
mcimadamore@1114 93 PREDEF("predef"),
mcimadamore@1114 94 OBJECT_INIT("object-init"),
mcimadamore@1114 95 INTERNAL("internal");
mcimadamore@1114 96
mcimadamore@1114 97 String opt;
mcimadamore@1114 98
mcimadamore@1114 99 private VerboseResolutionMode(String opt) {
mcimadamore@1114 100 this.opt = opt;
mcimadamore@1114 101 }
mcimadamore@1114 102
mcimadamore@1114 103 static EnumSet<VerboseResolutionMode> getVerboseResolutionMode(Options opts) {
mcimadamore@1114 104 String s = opts.get("verboseResolution");
mcimadamore@1114 105 EnumSet<VerboseResolutionMode> res = EnumSet.noneOf(VerboseResolutionMode.class);
mcimadamore@1114 106 if (s == null) return res;
mcimadamore@1114 107 if (s.contains("all")) {
mcimadamore@1114 108 res = EnumSet.allOf(VerboseResolutionMode.class);
mcimadamore@1114 109 }
mcimadamore@1114 110 Collection<String> args = Arrays.asList(s.split(","));
mcimadamore@1114 111 for (VerboseResolutionMode mode : values()) {
mcimadamore@1114 112 if (args.contains(mode.opt)) {
mcimadamore@1114 113 res.add(mode);
mcimadamore@1114 114 } else if (args.contains("-" + mode.opt)) {
mcimadamore@1114 115 res.remove(mode);
mcimadamore@1114 116 }
mcimadamore@1114 117 }
mcimadamore@1114 118 return res;
mcimadamore@1114 119 }
mcimadamore@1114 120 }
mcimadamore@1114 121
duke@1 122 public static Resolve instance(Context context) {
duke@1 123 Resolve instance = context.get(resolveKey);
duke@1 124 if (instance == null)
duke@1 125 instance = new Resolve(context);
duke@1 126 return instance;
duke@1 127 }
duke@1 128
duke@1 129 protected Resolve(Context context) {
duke@1 130 context.put(resolveKey, this);
duke@1 131 syms = Symtab.instance(context);
duke@1 132
duke@1 133 varNotFound = new
mcimadamore@302 134 SymbolNotFoundError(ABSENT_VAR);
duke@1 135 wrongMethod = new
mcimadamore@302 136 InapplicableSymbolError(syms.errSymbol);
duke@1 137 wrongMethods = new
mcimadamore@302 138 InapplicableSymbolsError(syms.errSymbol);
duke@1 139 methodNotFound = new
mcimadamore@302 140 SymbolNotFoundError(ABSENT_MTH);
duke@1 141 typeNotFound = new
mcimadamore@302 142 SymbolNotFoundError(ABSENT_TYP);
duke@1 143
jjg@113 144 names = Names.instance(context);
duke@1 145 log = Log.instance(context);
duke@1 146 chk = Check.instance(context);
duke@1 147 infer = Infer.instance(context);
duke@1 148 reader = ClassReader.instance(context);
duke@1 149 treeinfo = TreeInfo.instance(context);
duke@1 150 types = Types.instance(context);
mcimadamore@89 151 diags = JCDiagnostic.Factory.instance(context);
duke@1 152 Source source = Source.instance(context);
duke@1 153 boxingEnabled = source.allowBoxing();
duke@1 154 varargsEnabled = source.allowVarargs();
duke@1 155 Options options = Options.instance(context);
jjg@700 156 debugResolve = options.isSet("debugresolve");
mcimadamore@1114 157 verboseResolutionMode = VerboseResolutionMode.getVerboseResolutionMode(options);
mcimadamore@674 158 Target target = Target.instance(context);
mcimadamore@820 159 allowMethodHandles = target.hasMethodHandles();
mcimadamore@674 160 polymorphicSignatureScope = new Scope(syms.noSymbol);
mcimadamore@689 161
mcimadamore@689 162 inapplicableMethodException = new InapplicableMethodException(diags);
duke@1 163 }
duke@1 164
duke@1 165 /** error symbols, which are returned when resolution fails
duke@1 166 */
mcimadamore@302 167 final SymbolNotFoundError varNotFound;
mcimadamore@302 168 final InapplicableSymbolError wrongMethod;
mcimadamore@302 169 final InapplicableSymbolsError wrongMethods;
mcimadamore@302 170 final SymbolNotFoundError methodNotFound;
mcimadamore@302 171 final SymbolNotFoundError typeNotFound;
duke@1 172
duke@1 173 /* ************************************************************************
duke@1 174 * Identifier resolution
duke@1 175 *************************************************************************/
duke@1 176
duke@1 177 /** An environment is "static" if its static level is greater than
duke@1 178 * the one of its outer environment
duke@1 179 */
duke@1 180 static boolean isStatic(Env<AttrContext> env) {
duke@1 181 return env.info.staticLevel > env.outer.info.staticLevel;
duke@1 182 }
duke@1 183
duke@1 184 /** An environment is an "initializer" if it is a constructor or
duke@1 185 * an instance initializer.
duke@1 186 */
duke@1 187 static boolean isInitializer(Env<AttrContext> env) {
duke@1 188 Symbol owner = env.info.scope.owner;
duke@1 189 return owner.isConstructor() ||
duke@1 190 owner.owner.kind == TYP &&
duke@1 191 (owner.kind == VAR ||
duke@1 192 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
duke@1 193 (owner.flags() & STATIC) == 0;
duke@1 194 }
duke@1 195
duke@1 196 /** Is class accessible in given evironment?
duke@1 197 * @param env The current environment.
duke@1 198 * @param c The class whose accessibility is checked.
duke@1 199 */
duke@1 200 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
mcimadamore@741 201 return isAccessible(env, c, false);
mcimadamore@741 202 }
mcimadamore@741 203
mcimadamore@741 204 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c, boolean checkInner) {
mcimadamore@741 205 boolean isAccessible = false;
duke@1 206 switch ((short)(c.flags() & AccessFlags)) {
mcimadamore@741 207 case PRIVATE:
mcimadamore@741 208 isAccessible =
mcimadamore@741 209 env.enclClass.sym.outermostClass() ==
mcimadamore@741 210 c.owner.outermostClass();
mcimadamore@741 211 break;
mcimadamore@741 212 case 0:
mcimadamore@741 213 isAccessible =
mcimadamore@741 214 env.toplevel.packge == c.owner // fast special case
mcimadamore@741 215 ||
mcimadamore@741 216 env.toplevel.packge == c.packge()
mcimadamore@741 217 ||
mcimadamore@741 218 // Hack: this case is added since synthesized default constructors
mcimadamore@741 219 // of anonymous classes should be allowed to access
mcimadamore@741 220 // classes which would be inaccessible otherwise.
mcimadamore@741 221 env.enclMethod != null &&
mcimadamore@741 222 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
mcimadamore@741 223 break;
mcimadamore@741 224 default: // error recovery
mcimadamore@741 225 case PUBLIC:
mcimadamore@741 226 isAccessible = true;
mcimadamore@741 227 break;
mcimadamore@741 228 case PROTECTED:
mcimadamore@741 229 isAccessible =
mcimadamore@741 230 env.toplevel.packge == c.owner // fast special case
mcimadamore@741 231 ||
mcimadamore@741 232 env.toplevel.packge == c.packge()
mcimadamore@741 233 ||
mcimadamore@741 234 isInnerSubClass(env.enclClass.sym, c.owner);
mcimadamore@741 235 break;
duke@1 236 }
mcimadamore@741 237 return (checkInner == false || c.type.getEnclosingType() == Type.noType) ?
mcimadamore@741 238 isAccessible :
jjg@789 239 isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner);
duke@1 240 }
duke@1 241 //where
duke@1 242 /** Is given class a subclass of given base class, or an inner class
duke@1 243 * of a subclass?
duke@1 244 * Return null if no such class exists.
duke@1 245 * @param c The class which is the subclass or is contained in it.
duke@1 246 * @param base The base class
duke@1 247 */
duke@1 248 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
duke@1 249 while (c != null && !c.isSubClass(base, types)) {
duke@1 250 c = c.owner.enclClass();
duke@1 251 }
duke@1 252 return c != null;
duke@1 253 }
duke@1 254
duke@1 255 boolean isAccessible(Env<AttrContext> env, Type t) {
mcimadamore@741 256 return isAccessible(env, t, false);
mcimadamore@741 257 }
mcimadamore@741 258
mcimadamore@741 259 boolean isAccessible(Env<AttrContext> env, Type t, boolean checkInner) {
duke@1 260 return (t.tag == ARRAY)
duke@1 261 ? isAccessible(env, types.elemtype(t))
mcimadamore@741 262 : isAccessible(env, t.tsym, checkInner);
duke@1 263 }
duke@1 264
duke@1 265 /** Is symbol accessible as a member of given type in given evironment?
duke@1 266 * @param env The current environment.
duke@1 267 * @param site The type of which the tested symbol is regarded
duke@1 268 * as a member.
duke@1 269 * @param sym The symbol.
duke@1 270 */
duke@1 271 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
mcimadamore@741 272 return isAccessible(env, site, sym, false);
mcimadamore@741 273 }
mcimadamore@741 274 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) {
duke@1 275 if (sym.name == names.init && sym.owner != site.tsym) return false;
duke@1 276 switch ((short)(sym.flags() & AccessFlags)) {
duke@1 277 case PRIVATE:
duke@1 278 return
duke@1 279 (env.enclClass.sym == sym.owner // fast special case
duke@1 280 ||
duke@1 281 env.enclClass.sym.outermostClass() ==
duke@1 282 sym.owner.outermostClass())
duke@1 283 &&
duke@1 284 sym.isInheritedIn(site.tsym, types);
duke@1 285 case 0:
duke@1 286 return
duke@1 287 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 288 ||
duke@1 289 env.toplevel.packge == sym.packge())
duke@1 290 &&
mcimadamore@741 291 isAccessible(env, site, checkInner)
duke@1 292 &&
mcimadamore@254 293 sym.isInheritedIn(site.tsym, types)
mcimadamore@254 294 &&
mcimadamore@254 295 notOverriddenIn(site, sym);
duke@1 296 case PROTECTED:
duke@1 297 return
duke@1 298 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 299 ||
duke@1 300 env.toplevel.packge == sym.packge()
duke@1 301 ||
duke@1 302 isProtectedAccessible(sym, env.enclClass.sym, site)
duke@1 303 ||
duke@1 304 // OK to select instance method or field from 'super' or type name
duke@1 305 // (but type names should be disallowed elsewhere!)
duke@1 306 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
duke@1 307 &&
mcimadamore@741 308 isAccessible(env, site, checkInner)
duke@1 309 &&
mcimadamore@254 310 notOverriddenIn(site, sym);
duke@1 311 default: // this case includes erroneous combinations as well
mcimadamore@741 312 return isAccessible(env, site, checkInner) && notOverriddenIn(site, sym);
mcimadamore@254 313 }
mcimadamore@254 314 }
mcimadamore@254 315 //where
mcimadamore@254 316 /* `sym' is accessible only if not overridden by
mcimadamore@254 317 * another symbol which is a member of `site'
mcimadamore@254 318 * (because, if it is overridden, `sym' is not strictly
mcimadamore@674 319 * speaking a member of `site'). A polymorphic signature method
mcimadamore@674 320 * cannot be overridden (e.g. MH.invokeExact(Object[])).
mcimadamore@254 321 */
mcimadamore@254 322 private boolean notOverriddenIn(Type site, Symbol sym) {
mcimadamore@254 323 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
mcimadamore@254 324 return true;
mcimadamore@254 325 else {
mcimadamore@254 326 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
mcimadamore@844 327 return (s2 == null || s2 == sym || sym.owner == s2.owner ||
mcimadamore@674 328 s2.isPolymorphicSignatureGeneric() ||
mcimadamore@325 329 !types.isSubSignature(types.memberType(site, s2), types.memberType(site, sym)));
duke@1 330 }
duke@1 331 }
duke@1 332 //where
duke@1 333 /** Is given protected symbol accessible if it is selected from given site
duke@1 334 * and the selection takes place in given class?
duke@1 335 * @param sym The symbol with protected access
duke@1 336 * @param c The class where the access takes place
duke@1 337 * @site The type of the qualifier
duke@1 338 */
duke@1 339 private
duke@1 340 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
duke@1 341 while (c != null &&
duke@1 342 !(c.isSubClass(sym.owner, types) &&
duke@1 343 (c.flags() & INTERFACE) == 0 &&
duke@1 344 // In JLS 2e 6.6.2.1, the subclass restriction applies
duke@1 345 // only to instance fields and methods -- types are excluded
duke@1 346 // regardless of whether they are declared 'static' or not.
duke@1 347 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
duke@1 348 c = c.owner.enclClass();
duke@1 349 return c != null;
duke@1 350 }
duke@1 351
duke@1 352 /** Try to instantiate the type of a method so that it fits
duke@1 353 * given type arguments and argument types. If succesful, return
duke@1 354 * the method's instantiated type, else return null.
duke@1 355 * The instantiation will take into account an additional leading
duke@1 356 * formal parameter if the method is an instance method seen as a member
duke@1 357 * of un underdetermined site In this case, we treat site as an additional
duke@1 358 * parameter and the parameters of the class containing the method as
duke@1 359 * additional type variables that get instantiated.
duke@1 360 *
duke@1 361 * @param env The current environment
duke@1 362 * @param site The type of which the method is a member.
duke@1 363 * @param m The method symbol.
duke@1 364 * @param argtypes The invocation's given value arguments.
duke@1 365 * @param typeargtypes The invocation's given type arguments.
duke@1 366 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 367 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 368 */
duke@1 369 Type rawInstantiate(Env<AttrContext> env,
duke@1 370 Type site,
duke@1 371 Symbol m,
duke@1 372 List<Type> argtypes,
duke@1 373 List<Type> typeargtypes,
duke@1 374 boolean allowBoxing,
duke@1 375 boolean useVarargs,
duke@1 376 Warner warn)
mcimadamore@299 377 throws Infer.InferenceException {
mcimadamore@820 378 boolean polymorphicSignature = m.isPolymorphicSignatureGeneric() && allowMethodHandles;
mcimadamore@689 379 if (useVarargs && (m.flags() & VARARGS) == 0)
mcimadamore@845 380 throw inapplicableMethodException.setMessage();
duke@1 381 Type mt = types.memberType(site, m);
duke@1 382
duke@1 383 // tvars is the list of formal type variables for which type arguments
duke@1 384 // need to inferred.
mcimadamore@950 385 List<Type> tvars = null;
mcimadamore@950 386 if (env.info.tvars != null) {
mcimadamore@950 387 tvars = types.newInstances(env.info.tvars);
mcimadamore@950 388 mt = types.subst(mt, env.info.tvars, tvars);
mcimadamore@950 389 }
duke@1 390 if (typeargtypes == null) typeargtypes = List.nil();
mcimadamore@820 391 if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
duke@1 392 // This is not a polymorphic method, but typeargs are supplied
jjh@972 393 // which is fine, see JLS 15.12.2.1
duke@1 394 } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
duke@1 395 ForAll pmt = (ForAll) mt;
duke@1 396 if (typeargtypes.length() != pmt.tvars.length())
mcimadamore@689 397 throw inapplicableMethodException.setMessage("arg.length.mismatch"); // not enough args
duke@1 398 // Check type arguments are within bounds
duke@1 399 List<Type> formals = pmt.tvars;
duke@1 400 List<Type> actuals = typeargtypes;
duke@1 401 while (formals.nonEmpty() && actuals.nonEmpty()) {
duke@1 402 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
duke@1 403 pmt.tvars, typeargtypes);
duke@1 404 for (; bounds.nonEmpty(); bounds = bounds.tail)
duke@1 405 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
mcimadamore@689 406 throw inapplicableMethodException.setMessage("explicit.param.do.not.conform.to.bounds",actuals.head, bounds);
duke@1 407 formals = formals.tail;
duke@1 408 actuals = actuals.tail;
duke@1 409 }
duke@1 410 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
duke@1 411 } else if (mt.tag == FORALL) {
duke@1 412 ForAll pmt = (ForAll) mt;
duke@1 413 List<Type> tvars1 = types.newInstances(pmt.tvars);
duke@1 414 tvars = tvars.appendList(tvars1);
duke@1 415 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
duke@1 416 }
duke@1 417
duke@1 418 // find out whether we need to go the slow route via infer
mcimadamore@674 419 boolean instNeeded = tvars.tail != null || /*inlined: tvars.nonEmpty()*/
mcimadamore@674 420 polymorphicSignature;
duke@1 421 for (List<Type> l = argtypes;
duke@1 422 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
duke@1 423 l = l.tail) {
duke@1 424 if (l.head.tag == FORALL) instNeeded = true;
duke@1 425 }
duke@1 426
duke@1 427 if (instNeeded)
mcimadamore@674 428 return polymorphicSignature ?
mcimadamore@820 429 infer.instantiatePolymorphicSignatureInstance(env, site, m.name, (MethodSymbol)m, argtypes) :
mcimadamore@674 430 infer.instantiateMethod(env,
mcimadamore@547 431 tvars,
duke@1 432 (MethodType)mt,
mcimadamore@580 433 m,
duke@1 434 argtypes,
duke@1 435 allowBoxing,
duke@1 436 useVarargs,
duke@1 437 warn);
mcimadamore@689 438
mcimadamore@845 439 checkRawArgumentsAcceptable(env, argtypes, mt.getParameterTypes(),
mcimadamore@689 440 allowBoxing, useVarargs, warn);
mcimadamore@689 441 return mt;
duke@1 442 }
duke@1 443
duke@1 444 /** Same but returns null instead throwing a NoInstanceException
duke@1 445 */
duke@1 446 Type instantiate(Env<AttrContext> env,
duke@1 447 Type site,
duke@1 448 Symbol m,
duke@1 449 List<Type> argtypes,
duke@1 450 List<Type> typeargtypes,
duke@1 451 boolean allowBoxing,
duke@1 452 boolean useVarargs,
duke@1 453 Warner warn) {
duke@1 454 try {
duke@1 455 return rawInstantiate(env, site, m, argtypes, typeargtypes,
duke@1 456 allowBoxing, useVarargs, warn);
mcimadamore@689 457 } catch (InapplicableMethodException ex) {
duke@1 458 return null;
duke@1 459 }
duke@1 460 }
duke@1 461
duke@1 462 /** Check if a parameter list accepts a list of args.
duke@1 463 */
mcimadamore@845 464 boolean argumentsAcceptable(Env<AttrContext> env,
mcimadamore@845 465 List<Type> argtypes,
duke@1 466 List<Type> formals,
duke@1 467 boolean allowBoxing,
duke@1 468 boolean useVarargs,
duke@1 469 Warner warn) {
mcimadamore@689 470 try {
mcimadamore@845 471 checkRawArgumentsAcceptable(env, argtypes, formals, allowBoxing, useVarargs, warn);
mcimadamore@689 472 return true;
mcimadamore@689 473 } catch (InapplicableMethodException ex) {
mcimadamore@689 474 return false;
mcimadamore@689 475 }
mcimadamore@689 476 }
mcimadamore@1186 477 /**
mcimadamore@1186 478 * A check handler is used by the main method applicability routine in order
mcimadamore@1186 479 * to handle specific method applicability failures. It is assumed that a class
mcimadamore@1186 480 * implementing this interface should throw exceptions that are a subtype of
mcimadamore@1186 481 * InapplicableMethodException (see below). Such exception will terminate the
mcimadamore@1186 482 * method applicability check and propagate important info outwards (for the
mcimadamore@1186 483 * purpose of generating better diagnostics).
mcimadamore@1186 484 */
mcimadamore@1186 485 interface MethodCheckHandler {
mcimadamore@1186 486 /* The number of actuals and formals differ */
mcimadamore@1186 487 InapplicableMethodException arityMismatch();
mcimadamore@1186 488 /* An actual argument type does not conform to the corresponding formal type */
mcimadamore@1186 489 InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected);
mcimadamore@1186 490 /* The element type of a varargs is not accessible in the current context */
mcimadamore@1186 491 InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected);
mcimadamore@1186 492 }
mcimadamore@1186 493
mcimadamore@1186 494 /**
mcimadamore@1186 495 * Basic method check handler used within Resolve - all methods end up
mcimadamore@1186 496 * throwing InapplicableMethodException; a diagnostic fragment that describes
mcimadamore@1186 497 * the cause as to why the method is not applicable is set on the exception
mcimadamore@1186 498 * before it is thrown.
mcimadamore@1186 499 */
mcimadamore@1186 500 MethodCheckHandler resolveHandler = new MethodCheckHandler() {
mcimadamore@1186 501 public InapplicableMethodException arityMismatch() {
mcimadamore@1186 502 return inapplicableMethodException.setMessage("arg.length.mismatch");
mcimadamore@1186 503 }
mcimadamore@1186 504 public InapplicableMethodException argumentMismatch(boolean varargs, Type found, Type expected) {
mcimadamore@1186 505 String key = varargs ?
mcimadamore@1186 506 "varargs.argument.mismatch" :
mcimadamore@1186 507 "no.conforming.assignment.exists";
mcimadamore@1186 508 return inapplicableMethodException.setMessage(key,
mcimadamore@1186 509 found, expected);
mcimadamore@1186 510 }
mcimadamore@1186 511 public InapplicableMethodException inaccessibleVarargs(Symbol location, Type expected) {
mcimadamore@1186 512 return inapplicableMethodException.setMessage("inaccessible.varargs.type",
mcimadamore@1186 513 expected, Kinds.kindName(location), location);
mcimadamore@1186 514 }
mcimadamore@1186 515 };
mcimadamore@1186 516
mcimadamore@845 517 void checkRawArgumentsAcceptable(Env<AttrContext> env,
mcimadamore@845 518 List<Type> argtypes,
mcimadamore@689 519 List<Type> formals,
mcimadamore@689 520 boolean allowBoxing,
mcimadamore@689 521 boolean useVarargs,
mcimadamore@689 522 Warner warn) {
mcimadamore@1186 523 checkRawArgumentsAcceptable(env, List.<Type>nil(), argtypes, formals,
mcimadamore@1186 524 allowBoxing, useVarargs, warn, resolveHandler);
mcimadamore@1186 525 }
mcimadamore@1186 526
mcimadamore@1186 527 /**
mcimadamore@1186 528 * Main method applicability routine. Given a list of actual types A,
mcimadamore@1186 529 * a list of formal types F, determines whether the types in A are
mcimadamore@1186 530 * compatible (by method invocation conversion) with the types in F.
mcimadamore@1186 531 *
mcimadamore@1186 532 * Since this routine is shared between overload resolution and method
mcimadamore@1186 533 * type-inference, it is crucial that actual types are converted to the
mcimadamore@1186 534 * corresponding 'undet' form (i.e. where inference variables are replaced
mcimadamore@1186 535 * with undetvars) so that constraints can be propagated and collected.
mcimadamore@1186 536 *
mcimadamore@1186 537 * Moreover, if one or more types in A is a poly type, this routine calls
mcimadamore@1186 538 * Infer.instantiateArg in order to complete the poly type (this might involve
mcimadamore@1186 539 * deferred attribution).
mcimadamore@1186 540 *
mcimadamore@1186 541 * A method check handler (see above) is used in order to report errors.
mcimadamore@1186 542 */
mcimadamore@1186 543 List<Type> checkRawArgumentsAcceptable(Env<AttrContext> env,
mcimadamore@1186 544 List<Type> undetvars,
mcimadamore@1186 545 List<Type> argtypes,
mcimadamore@1186 546 List<Type> formals,
mcimadamore@1186 547 boolean allowBoxing,
mcimadamore@1186 548 boolean useVarargs,
mcimadamore@1186 549 Warner warn,
mcimadamore@1186 550 MethodCheckHandler handler) {
duke@1 551 Type varargsFormal = useVarargs ? formals.last() : null;
mcimadamore@1186 552 ListBuffer<Type> checkedArgs = ListBuffer.lb();
mcimadamore@1186 553
mcimadamore@689 554 if (varargsFormal == null &&
mcimadamore@689 555 argtypes.size() != formals.size()) {
mcimadamore@1186 556 throw handler.arityMismatch(); // not enough args
mcimadamore@689 557 }
mcimadamore@689 558
duke@1 559 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
mcimadamore@1186 560 Type undetFormal = infer.asUndetType(formals.head, undetvars);
mcimadamore@1186 561 Type capturedActual = types.capture(argtypes.head);
mcimadamore@1186 562 boolean works = allowBoxing ?
mcimadamore@1186 563 types.isConvertible(capturedActual, undetFormal, warn) :
mcimadamore@1186 564 types.isSubtypeUnchecked(capturedActual, undetFormal, warn);
mcimadamore@1186 565 if (!works) {
mcimadamore@1186 566 throw handler.argumentMismatch(false, argtypes.head, formals.head);
mcimadamore@1186 567 }
mcimadamore@1186 568 checkedArgs.append(capturedActual);
duke@1 569 argtypes = argtypes.tail;
duke@1 570 formals = formals.tail;
duke@1 571 }
mcimadamore@689 572
mcimadamore@1186 573 if (formals.head != varargsFormal) {
mcimadamore@1186 574 throw handler.arityMismatch(); // not enough args
mcimadamore@1186 575 }
mcimadamore@689 576
mcimadamore@689 577 if (useVarargs) {
mcimadamore@1186 578 //note: if applicability check is triggered by most specific test,
mcimadamore@1186 579 //the last argument of a varargs is _not_ an array type (see JLS 15.12.2.5)
mcimadamore@1006 580 Type elt = types.elemtype(varargsFormal);
mcimadamore@1186 581 Type eltUndet = infer.asUndetType(elt, undetvars);
mcimadamore@689 582 while (argtypes.nonEmpty()) {
mcimadamore@1186 583 Type capturedActual = types.capture(argtypes.head);
mcimadamore@1186 584 if (!types.isConvertible(capturedActual, eltUndet, warn)) {
mcimadamore@1186 585 throw handler.argumentMismatch(true, argtypes.head, elt);
mcimadamore@1186 586 }
mcimadamore@1186 587 checkedArgs.append(capturedActual);
mcimadamore@689 588 argtypes = argtypes.tail;
mcimadamore@689 589 }
mcimadamore@845 590 //check varargs element type accessibility
mcimadamore@1186 591 if (undetvars.isEmpty() && !isAccessible(env, elt)) {
mcimadamore@845 592 Symbol location = env.enclClass.sym;
mcimadamore@1186 593 throw handler.inaccessibleVarargs(location, elt);
mcimadamore@845 594 }
duke@1 595 }
mcimadamore@1186 596 return checkedArgs.toList();
duke@1 597 }
mcimadamore@689 598 // where
mcimadamore@689 599 public static class InapplicableMethodException extends RuntimeException {
mcimadamore@689 600 private static final long serialVersionUID = 0;
mcimadamore@689 601
mcimadamore@689 602 JCDiagnostic diagnostic;
mcimadamore@689 603 JCDiagnostic.Factory diags;
mcimadamore@689 604
mcimadamore@689 605 InapplicableMethodException(JCDiagnostic.Factory diags) {
mcimadamore@689 606 this.diagnostic = null;
mcimadamore@689 607 this.diags = diags;
mcimadamore@689 608 }
mcimadamore@845 609 InapplicableMethodException setMessage() {
mcimadamore@845 610 this.diagnostic = null;
mcimadamore@845 611 return this;
mcimadamore@845 612 }
mcimadamore@689 613 InapplicableMethodException setMessage(String key) {
mcimadamore@689 614 this.diagnostic = key != null ? diags.fragment(key) : null;
mcimadamore@689 615 return this;
mcimadamore@689 616 }
mcimadamore@689 617 InapplicableMethodException setMessage(String key, Object... args) {
mcimadamore@689 618 this.diagnostic = key != null ? diags.fragment(key, args) : null;
mcimadamore@689 619 return this;
mcimadamore@689 620 }
mcimadamore@845 621 InapplicableMethodException setMessage(JCDiagnostic diag) {
mcimadamore@845 622 this.diagnostic = diag;
mcimadamore@845 623 return this;
mcimadamore@845 624 }
mcimadamore@689 625
mcimadamore@689 626 public JCDiagnostic getDiagnostic() {
mcimadamore@689 627 return diagnostic;
mcimadamore@689 628 }
mcimadamore@689 629 }
mcimadamore@689 630 private final InapplicableMethodException inapplicableMethodException;
duke@1 631
duke@1 632 /* ***************************************************************************
duke@1 633 * Symbol lookup
duke@1 634 * the following naming conventions for arguments are used
duke@1 635 *
duke@1 636 * env is the environment where the symbol was mentioned
duke@1 637 * site is the type of which the symbol is a member
duke@1 638 * name is the symbol's name
duke@1 639 * if no arguments are given
duke@1 640 * argtypes are the value arguments, if we search for a method
duke@1 641 *
duke@1 642 * If no symbol was found, a ResolveError detailing the problem is returned.
duke@1 643 ****************************************************************************/
duke@1 644
duke@1 645 /** Find field. Synthetic fields are always skipped.
duke@1 646 * @param env The current environment.
duke@1 647 * @param site The original type from where the selection takes place.
duke@1 648 * @param name The name of the field.
duke@1 649 * @param c The class to search for the field. This is always
duke@1 650 * a superclass or implemented interface of site's class.
duke@1 651 */
duke@1 652 Symbol findField(Env<AttrContext> env,
duke@1 653 Type site,
duke@1 654 Name name,
duke@1 655 TypeSymbol c) {
mcimadamore@19 656 while (c.type.tag == TYPEVAR)
mcimadamore@19 657 c = c.type.getUpperBound().tsym;
duke@1 658 Symbol bestSoFar = varNotFound;
duke@1 659 Symbol sym;
duke@1 660 Scope.Entry e = c.members().lookup(name);
duke@1 661 while (e.scope != null) {
duke@1 662 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 663 return isAccessible(env, site, e.sym)
duke@1 664 ? e.sym : new AccessError(env, site, e.sym);
duke@1 665 }
duke@1 666 e = e.next();
duke@1 667 }
duke@1 668 Type st = types.supertype(c.type);
mcimadamore@19 669 if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
duke@1 670 sym = findField(env, site, name, st.tsym);
duke@1 671 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 672 }
duke@1 673 for (List<Type> l = types.interfaces(c.type);
duke@1 674 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 675 l = l.tail) {
duke@1 676 sym = findField(env, site, name, l.head.tsym);
duke@1 677 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 678 sym.owner != bestSoFar.owner)
duke@1 679 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 680 else if (sym.kind < bestSoFar.kind)
duke@1 681 bestSoFar = sym;
duke@1 682 }
duke@1 683 return bestSoFar;
duke@1 684 }
duke@1 685
duke@1 686 /** Resolve a field identifier, throw a fatal error if not found.
duke@1 687 * @param pos The position to use for error reporting.
duke@1 688 * @param env The environment current at the method invocation.
duke@1 689 * @param site The type of the qualifying expression, in which
duke@1 690 * identifier is searched.
duke@1 691 * @param name The identifier's name.
duke@1 692 */
duke@1 693 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 694 Type site, Name name) {
duke@1 695 Symbol sym = findField(env, site, name, site.tsym);
duke@1 696 if (sym.kind == VAR) return (VarSymbol)sym;
duke@1 697 else throw new FatalError(
mcimadamore@89 698 diags.fragment("fatal.err.cant.locate.field",
duke@1 699 name));
duke@1 700 }
duke@1 701
duke@1 702 /** Find unqualified variable or field with given name.
duke@1 703 * Synthetic fields always skipped.
duke@1 704 * @param env The current environment.
duke@1 705 * @param name The name of the variable or field.
duke@1 706 */
duke@1 707 Symbol findVar(Env<AttrContext> env, Name name) {
duke@1 708 Symbol bestSoFar = varNotFound;
duke@1 709 Symbol sym;
duke@1 710 Env<AttrContext> env1 = env;
duke@1 711 boolean staticOnly = false;
duke@1 712 while (env1.outer != null) {
duke@1 713 if (isStatic(env1)) staticOnly = true;
duke@1 714 Scope.Entry e = env1.info.scope.lookup(name);
duke@1 715 while (e.scope != null &&
duke@1 716 (e.sym.kind != VAR ||
duke@1 717 (e.sym.flags_field & SYNTHETIC) != 0))
duke@1 718 e = e.next();
duke@1 719 sym = (e.scope != null)
duke@1 720 ? e.sym
duke@1 721 : findField(
duke@1 722 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
duke@1 723 if (sym.exists()) {
duke@1 724 if (staticOnly &&
duke@1 725 sym.kind == VAR &&
duke@1 726 sym.owner.kind == TYP &&
duke@1 727 (sym.flags() & STATIC) == 0)
duke@1 728 return new StaticError(sym);
duke@1 729 else
duke@1 730 return sym;
duke@1 731 } else if (sym.kind < bestSoFar.kind) {
duke@1 732 bestSoFar = sym;
duke@1 733 }
duke@1 734
duke@1 735 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 736 env1 = env1.outer;
duke@1 737 }
duke@1 738
duke@1 739 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
duke@1 740 if (sym.exists())
duke@1 741 return sym;
duke@1 742 if (bestSoFar.exists())
duke@1 743 return bestSoFar;
duke@1 744
duke@1 745 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 746 for (; e.scope != null; e = e.next()) {
duke@1 747 sym = e.sym;
duke@1 748 Type origin = e.getOrigin().owner.type;
duke@1 749 if (sym.kind == VAR) {
duke@1 750 if (e.sym.owner.type != origin)
duke@1 751 sym = sym.clone(e.getOrigin().owner);
duke@1 752 return isAccessible(env, origin, sym)
duke@1 753 ? sym : new AccessError(env, origin, sym);
duke@1 754 }
duke@1 755 }
duke@1 756
duke@1 757 Symbol origin = null;
duke@1 758 e = env.toplevel.starImportScope.lookup(name);
duke@1 759 for (; e.scope != null; e = e.next()) {
duke@1 760 sym = e.sym;
duke@1 761 if (sym.kind != VAR)
duke@1 762 continue;
duke@1 763 // invariant: sym.kind == VAR
duke@1 764 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
duke@1 765 return new AmbiguityError(bestSoFar, sym);
duke@1 766 else if (bestSoFar.kind >= VAR) {
duke@1 767 origin = e.getOrigin().owner;
duke@1 768 bestSoFar = isAccessible(env, origin.type, sym)
duke@1 769 ? sym : new AccessError(env, origin.type, sym);
duke@1 770 }
duke@1 771 }
duke@1 772 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
duke@1 773 return bestSoFar.clone(origin);
duke@1 774 else
duke@1 775 return bestSoFar;
duke@1 776 }
duke@1 777
duke@1 778 Warner noteWarner = new Warner();
duke@1 779
duke@1 780 /** Select the best method for a call site among two choices.
duke@1 781 * @param env The current environment.
duke@1 782 * @param site The original type from where the
duke@1 783 * selection takes place.
duke@1 784 * @param argtypes The invocation's value arguments,
duke@1 785 * @param typeargtypes The invocation's type arguments,
duke@1 786 * @param sym Proposed new best match.
duke@1 787 * @param bestSoFar Previously found best match.
duke@1 788 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 789 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 790 */
mcimadamore@689 791 @SuppressWarnings("fallthrough")
duke@1 792 Symbol selectBest(Env<AttrContext> env,
duke@1 793 Type site,
duke@1 794 List<Type> argtypes,
duke@1 795 List<Type> typeargtypes,
duke@1 796 Symbol sym,
duke@1 797 Symbol bestSoFar,
duke@1 798 boolean allowBoxing,
duke@1 799 boolean useVarargs,
duke@1 800 boolean operator) {
duke@1 801 if (sym.kind == ERR) return bestSoFar;
mcimadamore@171 802 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
jjg@816 803 Assert.check(sym.kind < AMBIGUOUS);
duke@1 804 try {
mcimadamore@1114 805 Type mt = rawInstantiate(env, site, sym, argtypes, typeargtypes,
mcimadamore@689 806 allowBoxing, useVarargs, Warner.noWarnings);
mcimadamore@1114 807 if (!operator) addVerboseApplicableCandidateDiag(sym ,mt);
mcimadamore@689 808 } catch (InapplicableMethodException ex) {
mcimadamore@1114 809 if (!operator) addVerboseInapplicableCandidateDiag(sym, ex.getDiagnostic());
duke@1 810 switch (bestSoFar.kind) {
duke@1 811 case ABSENT_MTH:
duke@1 812 return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
duke@1 813 case WRONG_MTH:
mcimadamore@1110 814 if (operator) return bestSoFar;
mcimadamore@689 815 wrongMethods.addCandidate(currentStep, wrongMethod.sym, wrongMethod.explanation);
mcimadamore@689 816 case WRONG_MTHS:
mcimadamore@689 817 return wrongMethods.addCandidate(currentStep, sym, ex.getDiagnostic());
duke@1 818 default:
duke@1 819 return bestSoFar;
duke@1 820 }
duke@1 821 }
duke@1 822 if (!isAccessible(env, site, sym)) {
duke@1 823 return (bestSoFar.kind == ABSENT_MTH)
duke@1 824 ? new AccessError(env, site, sym)
duke@1 825 : bestSoFar;
mcimadamore@689 826 }
duke@1 827 return (bestSoFar.kind > AMBIGUOUS)
duke@1 828 ? sym
duke@1 829 : mostSpecific(sym, bestSoFar, env, site,
duke@1 830 allowBoxing && operator, useVarargs);
duke@1 831 }
mcimadamore@1114 832 //where
mcimadamore@1114 833 void addVerboseApplicableCandidateDiag(Symbol sym, Type inst) {
mcimadamore@1114 834 if (!verboseResolutionMode.contains(VerboseResolutionMode.APPLICABLE))
mcimadamore@1114 835 return;
mcimadamore@1114 836
mcimadamore@1114 837 JCDiagnostic subDiag = null;
mcimadamore@1114 838 if (inst.getReturnType().tag == FORALL) {
mcimadamore@1114 839 Type diagType = types.createMethodTypeWithReturn(inst.asMethodType(),
mcimadamore@1114 840 ((ForAll)inst.getReturnType()).qtype);
mcimadamore@1114 841 subDiag = diags.fragment("partial.inst.sig", diagType);
mcimadamore@1114 842 } else if (sym.type.tag == FORALL) {
mcimadamore@1114 843 subDiag = diags.fragment("full.inst.sig", inst.asMethodType());
mcimadamore@1114 844 }
mcimadamore@1114 845
mcimadamore@1114 846 String key = subDiag == null ?
mcimadamore@1114 847 "applicable.method.found" :
mcimadamore@1114 848 "applicable.method.found.1";
mcimadamore@1114 849
mcimadamore@1114 850 verboseResolutionCandidateDiags.put(sym,
mcimadamore@1114 851 diags.fragment(key, verboseResolutionCandidateDiags.size(), sym, subDiag));
mcimadamore@1114 852 }
mcimadamore@1114 853
mcimadamore@1114 854 void addVerboseInapplicableCandidateDiag(Symbol sym, JCDiagnostic subDiag) {
mcimadamore@1114 855 if (!verboseResolutionMode.contains(VerboseResolutionMode.INAPPLICABLE))
mcimadamore@1114 856 return;
mcimadamore@1114 857 verboseResolutionCandidateDiags.put(sym,
mcimadamore@1114 858 diags.fragment("not.applicable.method.found", verboseResolutionCandidateDiags.size(), sym, subDiag));
mcimadamore@1114 859 }
duke@1 860
duke@1 861 /* Return the most specific of the two methods for a call,
duke@1 862 * given that both are accessible and applicable.
duke@1 863 * @param m1 A new candidate for most specific.
duke@1 864 * @param m2 The previous most specific candidate.
duke@1 865 * @param env The current environment.
duke@1 866 * @param site The original type from where the selection
duke@1 867 * takes place.
duke@1 868 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 869 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 870 */
duke@1 871 Symbol mostSpecific(Symbol m1,
duke@1 872 Symbol m2,
duke@1 873 Env<AttrContext> env,
mcimadamore@254 874 final Type site,
duke@1 875 boolean allowBoxing,
duke@1 876 boolean useVarargs) {
duke@1 877 switch (m2.kind) {
duke@1 878 case MTH:
duke@1 879 if (m1 == m2) return m1;
mcimadamore@775 880 boolean m1SignatureMoreSpecific = signatureMoreSpecific(env, site, m1, m2, allowBoxing, useVarargs);
mcimadamore@775 881 boolean m2SignatureMoreSpecific = signatureMoreSpecific(env, site, m2, m1, allowBoxing, useVarargs);
duke@1 882 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
mcimadamore@775 883 Type mt1 = types.memberType(site, m1);
mcimadamore@775 884 Type mt2 = types.memberType(site, m2);
duke@1 885 if (!types.overrideEquivalent(mt1, mt2))
mcimadamore@844 886 return ambiguityError(m1, m2);
mcimadamore@844 887
duke@1 888 // same signature; select (a) the non-bridge method, or
duke@1 889 // (b) the one that overrides the other, or (c) the concrete
duke@1 890 // one, or (d) merge both abstract signatures
mcimadamore@844 891 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE))
duke@1 892 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
mcimadamore@844 893
duke@1 894 // if one overrides or hides the other, use it
duke@1 895 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
duke@1 896 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
duke@1 897 if (types.asSuper(m1Owner.type, m2Owner) != null &&
duke@1 898 ((m1.owner.flags_field & INTERFACE) == 0 ||
duke@1 899 (m2.owner.flags_field & INTERFACE) != 0) &&
duke@1 900 m1.overrides(m2, m1Owner, types, false))
duke@1 901 return m1;
duke@1 902 if (types.asSuper(m2Owner.type, m1Owner) != null &&
duke@1 903 ((m2.owner.flags_field & INTERFACE) == 0 ||
duke@1 904 (m1.owner.flags_field & INTERFACE) != 0) &&
duke@1 905 m2.overrides(m1, m2Owner, types, false))
duke@1 906 return m2;
duke@1 907 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
duke@1 908 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
duke@1 909 if (m1Abstract && !m2Abstract) return m2;
duke@1 910 if (m2Abstract && !m1Abstract) return m1;
duke@1 911 // both abstract or both concrete
duke@1 912 if (!m1Abstract && !m2Abstract)
mcimadamore@844 913 return ambiguityError(m1, m2);
mcimadamore@156 914 // check that both signatures have the same erasure
mcimadamore@156 915 if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
mcimadamore@156 916 m2.erasure(types).getParameterTypes()))
mcimadamore@844 917 return ambiguityError(m1, m2);
duke@1 918 // both abstract, neither overridden; merge throws clause and result type
mcimadamore@1059 919 Type mst = mostSpecificReturnType(mt1, mt2);
mcimadamore@1059 920 if (mst == null) {
duke@1 921 // Theoretically, this can't happen, but it is possible
duke@1 922 // due to error recovery or mixing incompatible class files
mcimadamore@844 923 return ambiguityError(m1, m2);
duke@1 924 }
mcimadamore@1059 925 Symbol mostSpecific = mst == mt1 ? m1 : m2;
dlsmith@880 926 List<Type> allThrown = chk.intersect(mt1.getThrownTypes(), mt2.getThrownTypes());
dlsmith@880 927 Type newSig = types.createMethodTypeWithThrown(mostSpecific.type, allThrown);
mcimadamore@254 928 MethodSymbol result = new MethodSymbol(
mcimadamore@254 929 mostSpecific.flags(),
mcimadamore@254 930 mostSpecific.name,
dlsmith@880 931 newSig,
mcimadamore@254 932 mostSpecific.owner) {
mcimadamore@254 933 @Override
mcimadamore@254 934 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@254 935 if (origin == site.tsym)
mcimadamore@254 936 return this;
mcimadamore@254 937 else
mcimadamore@254 938 return super.implementation(origin, types, checkResult);
mcimadamore@254 939 }
mcimadamore@254 940 };
duke@1 941 return result;
duke@1 942 }
duke@1 943 if (m1SignatureMoreSpecific) return m1;
duke@1 944 if (m2SignatureMoreSpecific) return m2;
mcimadamore@844 945 return ambiguityError(m1, m2);
duke@1 946 case AMBIGUOUS:
duke@1 947 AmbiguityError e = (AmbiguityError)m2;
mcimadamore@302 948 Symbol err1 = mostSpecific(m1, e.sym, env, site, allowBoxing, useVarargs);
duke@1 949 Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
duke@1 950 if (err1 == err2) return err1;
mcimadamore@302 951 if (err1 == e.sym && err2 == e.sym2) return m2;
duke@1 952 if (err1 instanceof AmbiguityError &&
duke@1 953 err2 instanceof AmbiguityError &&
mcimadamore@302 954 ((AmbiguityError)err1).sym == ((AmbiguityError)err2).sym)
mcimadamore@844 955 return ambiguityError(m1, m2);
duke@1 956 else
mcimadamore@844 957 return ambiguityError(err1, err2);
duke@1 958 default:
duke@1 959 throw new AssertionError();
duke@1 960 }
duke@1 961 }
mcimadamore@775 962 //where
mcimadamore@775 963 private boolean signatureMoreSpecific(Env<AttrContext> env, Type site, Symbol m1, Symbol m2, boolean allowBoxing, boolean useVarargs) {
mcimadamore@795 964 noteWarner.clear();
mcimadamore@775 965 Type mtype1 = types.memberType(site, adjustVarargs(m1, m2, useVarargs));
mcimadamore@1006 966 Type mtype2 = instantiate(env, site, adjustVarargs(m2, m1, useVarargs),
mcimadamore@1006 967 types.lowerBoundArgtypes(mtype1), null,
mcimadamore@1006 968 allowBoxing, false, noteWarner);
mcimadamore@1006 969 return mtype2 != null &&
mcimadamore@795 970 !noteWarner.hasLint(Lint.LintCategory.UNCHECKED);
mcimadamore@775 971 }
mcimadamore@775 972 //where
mcimadamore@775 973 private Symbol adjustVarargs(Symbol to, Symbol from, boolean useVarargs) {
mcimadamore@775 974 List<Type> fromArgs = from.type.getParameterTypes();
mcimadamore@775 975 List<Type> toArgs = to.type.getParameterTypes();
mcimadamore@775 976 if (useVarargs &&
mcimadamore@775 977 (from.flags() & VARARGS) != 0 &&
mcimadamore@775 978 (to.flags() & VARARGS) != 0) {
mcimadamore@775 979 Type varargsTypeFrom = fromArgs.last();
mcimadamore@775 980 Type varargsTypeTo = toArgs.last();
mcimadamore@787 981 ListBuffer<Type> args = ListBuffer.lb();
mcimadamore@787 982 if (toArgs.length() < fromArgs.length()) {
mcimadamore@787 983 //if we are checking a varargs method 'from' against another varargs
mcimadamore@787 984 //method 'to' (where arity of 'to' < arity of 'from') then expand signature
mcimadamore@787 985 //of 'to' to 'fit' arity of 'from' (this means adding fake formals to 'to'
mcimadamore@787 986 //until 'to' signature has the same arity as 'from')
mcimadamore@787 987 while (fromArgs.head != varargsTypeFrom) {
mcimadamore@787 988 args.append(toArgs.head == varargsTypeTo ? types.elemtype(varargsTypeTo) : toArgs.head);
mcimadamore@787 989 fromArgs = fromArgs.tail;
mcimadamore@787 990 toArgs = toArgs.head == varargsTypeTo ?
mcimadamore@787 991 toArgs :
mcimadamore@787 992 toArgs.tail;
mcimadamore@787 993 }
mcimadamore@787 994 } else {
mcimadamore@787 995 //formal argument list is same as original list where last
mcimadamore@787 996 //argument (array type) is removed
mcimadamore@787 997 args.appendList(toArgs.reverse().tail.reverse());
mcimadamore@775 998 }
mcimadamore@787 999 //append varargs element type as last synthetic formal
mcimadamore@787 1000 args.append(types.elemtype(varargsTypeTo));
dlsmith@880 1001 Type mtype = types.createMethodTypeWithParameters(to.type, args.toList());
mcimadamore@1006 1002 return new MethodSymbol(to.flags_field & ~VARARGS, to.name, mtype, to.owner);
mcimadamore@775 1003 } else {
mcimadamore@775 1004 return to;
mcimadamore@775 1005 }
mcimadamore@775 1006 }
mcimadamore@844 1007 //where
mcimadamore@1059 1008 Type mostSpecificReturnType(Type mt1, Type mt2) {
mcimadamore@1059 1009 Type rt1 = mt1.getReturnType();
mcimadamore@1059 1010 Type rt2 = mt2.getReturnType();
mcimadamore@1059 1011
mcimadamore@1059 1012 if (mt1.tag == FORALL && mt2.tag == FORALL) {
mcimadamore@1059 1013 //if both are generic methods, adjust return type ahead of subtyping check
mcimadamore@1059 1014 rt1 = types.subst(rt1, mt1.getTypeArguments(), mt2.getTypeArguments());
mcimadamore@1059 1015 }
mcimadamore@1059 1016 //first use subtyping, then return type substitutability
mcimadamore@1059 1017 if (types.isSubtype(rt1, rt2)) {
mcimadamore@1059 1018 return mt1;
mcimadamore@1059 1019 } else if (types.isSubtype(rt2, rt1)) {
mcimadamore@1059 1020 return mt2;
mcimadamore@1059 1021 } else if (types.returnTypeSubstitutable(mt1, mt2)) {
mcimadamore@1059 1022 return mt1;
mcimadamore@1059 1023 } else if (types.returnTypeSubstitutable(mt2, mt1)) {
mcimadamore@1059 1024 return mt2;
mcimadamore@1059 1025 } else {
mcimadamore@1059 1026 return null;
mcimadamore@1059 1027 }
mcimadamore@1059 1028 }
mcimadamore@1059 1029 //where
mcimadamore@844 1030 Symbol ambiguityError(Symbol m1, Symbol m2) {
mcimadamore@844 1031 if (((m1.flags() | m2.flags()) & CLASH) != 0) {
mcimadamore@844 1032 return (m1.flags() & CLASH) == 0 ? m1 : m2;
mcimadamore@844 1033 } else {
mcimadamore@844 1034 return new AmbiguityError(m1, m2);
mcimadamore@844 1035 }
mcimadamore@844 1036 }
duke@1 1037
duke@1 1038 /** Find best qualified method matching given name, type and value
duke@1 1039 * arguments.
duke@1 1040 * @param env The current environment.
duke@1 1041 * @param site The original type from where the selection
duke@1 1042 * takes place.
duke@1 1043 * @param name The method's name.
duke@1 1044 * @param argtypes The method's value arguments.
duke@1 1045 * @param typeargtypes The method's type arguments
duke@1 1046 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 1047 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1048 */
duke@1 1049 Symbol findMethod(Env<AttrContext> env,
duke@1 1050 Type site,
duke@1 1051 Name name,
duke@1 1052 List<Type> argtypes,
duke@1 1053 List<Type> typeargtypes,
duke@1 1054 boolean allowBoxing,
duke@1 1055 boolean useVarargs,
duke@1 1056 boolean operator) {
mcimadamore@1114 1057 verboseResolutionCandidateDiags.clear();
jrose@571 1058 Symbol bestSoFar = methodNotFound;
mcimadamore@1114 1059 bestSoFar = findMethod(env,
duke@1 1060 site,
duke@1 1061 name,
duke@1 1062 argtypes,
duke@1 1063 typeargtypes,
duke@1 1064 site.tsym.type,
duke@1 1065 true,
jrose@571 1066 bestSoFar,
duke@1 1067 allowBoxing,
duke@1 1068 useVarargs,
mcimadamore@913 1069 operator,
mcimadamore@913 1070 new HashSet<TypeSymbol>());
mcimadamore@1114 1071 reportVerboseResolutionDiagnostic(env.tree.pos(), name, site, argtypes, typeargtypes, bestSoFar);
mcimadamore@1114 1072 return bestSoFar;
duke@1 1073 }
duke@1 1074 // where
duke@1 1075 private Symbol findMethod(Env<AttrContext> env,
duke@1 1076 Type site,
duke@1 1077 Name name,
duke@1 1078 List<Type> argtypes,
duke@1 1079 List<Type> typeargtypes,
duke@1 1080 Type intype,
duke@1 1081 boolean abstractok,
duke@1 1082 Symbol bestSoFar,
duke@1 1083 boolean allowBoxing,
duke@1 1084 boolean useVarargs,
mcimadamore@913 1085 boolean operator,
mcimadamore@913 1086 Set<TypeSymbol> seen) {
mcimadamore@19 1087 for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) {
mcimadamore@19 1088 while (ct.tag == TYPEVAR)
mcimadamore@19 1089 ct = ct.getUpperBound();
duke@1 1090 ClassSymbol c = (ClassSymbol)ct.tsym;
mcimadamore@913 1091 if (!seen.add(c)) return bestSoFar;
mcimadamore@135 1092 if ((c.flags() & (ABSTRACT | INTERFACE | ENUM)) == 0)
duke@1 1093 abstractok = false;
duke@1 1094 for (Scope.Entry e = c.members().lookup(name);
duke@1 1095 e.scope != null;
duke@1 1096 e = e.next()) {
duke@1 1097 //- System.out.println(" e " + e.sym);
duke@1 1098 if (e.sym.kind == MTH &&
duke@1 1099 (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 1100 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
duke@1 1101 e.sym, bestSoFar,
duke@1 1102 allowBoxing,
duke@1 1103 useVarargs,
duke@1 1104 operator);
duke@1 1105 }
duke@1 1106 }
mcimadamore@537 1107 if (name == names.init)
mcimadamore@537 1108 break;
duke@1 1109 //- System.out.println(" - " + bestSoFar);
duke@1 1110 if (abstractok) {
duke@1 1111 Symbol concrete = methodNotFound;
duke@1 1112 if ((bestSoFar.flags() & ABSTRACT) == 0)
duke@1 1113 concrete = bestSoFar;
duke@1 1114 for (List<Type> l = types.interfaces(c.type);
duke@1 1115 l.nonEmpty();
duke@1 1116 l = l.tail) {
duke@1 1117 bestSoFar = findMethod(env, site, name, argtypes,
duke@1 1118 typeargtypes,
duke@1 1119 l.head, abstractok, bestSoFar,
mcimadamore@913 1120 allowBoxing, useVarargs, operator, seen);
duke@1 1121 }
duke@1 1122 if (concrete != bestSoFar &&
duke@1 1123 concrete.kind < ERR && bestSoFar.kind < ERR &&
duke@1 1124 types.isSubSignature(concrete.type, bestSoFar.type))
duke@1 1125 bestSoFar = concrete;
duke@1 1126 }
duke@1 1127 }
duke@1 1128 return bestSoFar;
duke@1 1129 }
mcimadamore@1114 1130 //where
mcimadamore@1114 1131 void reportVerboseResolutionDiagnostic(DiagnosticPosition dpos, Name name, Type site, List<Type> argtypes, List<Type> typeargtypes, Symbol bestSoFar) {
mcimadamore@1114 1132 boolean success = bestSoFar.kind < ERRONEOUS;
mcimadamore@1114 1133
mcimadamore@1114 1134 if (success && !verboseResolutionMode.contains(VerboseResolutionMode.SUCCESS)) {
mcimadamore@1114 1135 return;
mcimadamore@1114 1136 } else if (!success && !verboseResolutionMode.contains(VerboseResolutionMode.FAILURE)) {
mcimadamore@1114 1137 return;
mcimadamore@1114 1138 }
mcimadamore@1114 1139
mcimadamore@1114 1140 if (bestSoFar.name == names.init &&
mcimadamore@1114 1141 bestSoFar.owner == syms.objectType.tsym &&
mcimadamore@1114 1142 !verboseResolutionMode.contains(VerboseResolutionMode.OBJECT_INIT)) {
mcimadamore@1114 1143 return; //skip diags for Object constructor resolution
mcimadamore@1114 1144 } else if (site == syms.predefClass.type && !verboseResolutionMode.contains(VerboseResolutionMode.PREDEF)) {
mcimadamore@1114 1145 return; //skip spurious diags for predef symbols (i.e. operators)
mcimadamore@1114 1146 } else if (internalResolution && !verboseResolutionMode.contains(VerboseResolutionMode.INTERNAL)) {
mcimadamore@1114 1147 return;
mcimadamore@1114 1148 }
mcimadamore@1114 1149
mcimadamore@1114 1150 int pos = 0;
mcimadamore@1114 1151 for (Symbol s : verboseResolutionCandidateDiags.keySet()) {
mcimadamore@1114 1152 if (s == bestSoFar) break;
mcimadamore@1114 1153 pos++;
mcimadamore@1114 1154 }
mcimadamore@1114 1155 String key = success ? "verbose.resolve.multi" : "verbose.resolve.multi.1";
mcimadamore@1114 1156 JCDiagnostic main = diags.note(log.currentSource(), dpos, key, name, site.tsym, pos, currentStep,
mcimadamore@1114 1157 methodArguments(argtypes), methodArguments(typeargtypes));
mcimadamore@1114 1158 JCDiagnostic d = new JCDiagnostic.MultilineDiagnostic(main, List.from(verboseResolutionCandidateDiags.values().toArray(new JCDiagnostic[verboseResolutionCandidateDiags.size()])));
mcimadamore@1114 1159 log.report(d);
mcimadamore@1114 1160 }
duke@1 1161
duke@1 1162 /** Find unqualified method matching given name, type and value arguments.
duke@1 1163 * @param env The current environment.
duke@1 1164 * @param name The method's name.
duke@1 1165 * @param argtypes The method's value arguments.
duke@1 1166 * @param typeargtypes The method's type arguments.
duke@1 1167 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 1168 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1169 */
duke@1 1170 Symbol findFun(Env<AttrContext> env, Name name,
duke@1 1171 List<Type> argtypes, List<Type> typeargtypes,
duke@1 1172 boolean allowBoxing, boolean useVarargs) {
duke@1 1173 Symbol bestSoFar = methodNotFound;
duke@1 1174 Symbol sym;
duke@1 1175 Env<AttrContext> env1 = env;
duke@1 1176 boolean staticOnly = false;
duke@1 1177 while (env1.outer != null) {
duke@1 1178 if (isStatic(env1)) staticOnly = true;
duke@1 1179 sym = findMethod(
duke@1 1180 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
duke@1 1181 allowBoxing, useVarargs, false);
duke@1 1182 if (sym.exists()) {
duke@1 1183 if (staticOnly &&
duke@1 1184 sym.kind == MTH &&
duke@1 1185 sym.owner.kind == TYP &&
duke@1 1186 (sym.flags() & STATIC) == 0) return new StaticError(sym);
duke@1 1187 else return sym;
duke@1 1188 } else if (sym.kind < bestSoFar.kind) {
duke@1 1189 bestSoFar = sym;
duke@1 1190 }
duke@1 1191 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 1192 env1 = env1.outer;
duke@1 1193 }
duke@1 1194
duke@1 1195 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1196 typeargtypes, allowBoxing, useVarargs, false);
duke@1 1197 if (sym.exists())
duke@1 1198 return sym;
duke@1 1199
duke@1 1200 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 1201 for (; e.scope != null; e = e.next()) {
duke@1 1202 sym = e.sym;
duke@1 1203 Type origin = e.getOrigin().owner.type;
duke@1 1204 if (sym.kind == MTH) {
duke@1 1205 if (e.sym.owner.type != origin)
duke@1 1206 sym = sym.clone(e.getOrigin().owner);
duke@1 1207 if (!isAccessible(env, origin, sym))
duke@1 1208 sym = new AccessError(env, origin, sym);
duke@1 1209 bestSoFar = selectBest(env, origin,
duke@1 1210 argtypes, typeargtypes,
duke@1 1211 sym, bestSoFar,
duke@1 1212 allowBoxing, useVarargs, false);
duke@1 1213 }
duke@1 1214 }
duke@1 1215 if (bestSoFar.exists())
duke@1 1216 return bestSoFar;
duke@1 1217
duke@1 1218 e = env.toplevel.starImportScope.lookup(name);
duke@1 1219 for (; e.scope != null; e = e.next()) {
duke@1 1220 sym = e.sym;
duke@1 1221 Type origin = e.getOrigin().owner.type;
duke@1 1222 if (sym.kind == MTH) {
duke@1 1223 if (e.sym.owner.type != origin)
duke@1 1224 sym = sym.clone(e.getOrigin().owner);
duke@1 1225 if (!isAccessible(env, origin, sym))
duke@1 1226 sym = new AccessError(env, origin, sym);
duke@1 1227 bestSoFar = selectBest(env, origin,
duke@1 1228 argtypes, typeargtypes,
duke@1 1229 sym, bestSoFar,
duke@1 1230 allowBoxing, useVarargs, false);
duke@1 1231 }
duke@1 1232 }
duke@1 1233 return bestSoFar;
duke@1 1234 }
duke@1 1235
duke@1 1236 /** Load toplevel or member class with given fully qualified name and
duke@1 1237 * verify that it is accessible.
duke@1 1238 * @param env The current environment.
duke@1 1239 * @param name The fully qualified name of the class to be loaded.
duke@1 1240 */
duke@1 1241 Symbol loadClass(Env<AttrContext> env, Name name) {
duke@1 1242 try {
duke@1 1243 ClassSymbol c = reader.loadClass(name);
duke@1 1244 return isAccessible(env, c) ? c : new AccessError(c);
duke@1 1245 } catch (ClassReader.BadClassFile err) {
duke@1 1246 throw err;
duke@1 1247 } catch (CompletionFailure ex) {
duke@1 1248 return typeNotFound;
duke@1 1249 }
duke@1 1250 }
duke@1 1251
duke@1 1252 /** Find qualified member type.
duke@1 1253 * @param env The current environment.
duke@1 1254 * @param site The original type from where the selection takes
duke@1 1255 * place.
duke@1 1256 * @param name The type's name.
duke@1 1257 * @param c The class to search for the member type. This is
duke@1 1258 * always a superclass or implemented interface of
duke@1 1259 * site's class.
duke@1 1260 */
duke@1 1261 Symbol findMemberType(Env<AttrContext> env,
duke@1 1262 Type site,
duke@1 1263 Name name,
duke@1 1264 TypeSymbol c) {
duke@1 1265 Symbol bestSoFar = typeNotFound;
duke@1 1266 Symbol sym;
duke@1 1267 Scope.Entry e = c.members().lookup(name);
duke@1 1268 while (e.scope != null) {
duke@1 1269 if (e.sym.kind == TYP) {
duke@1 1270 return isAccessible(env, site, e.sym)
duke@1 1271 ? e.sym
duke@1 1272 : new AccessError(env, site, e.sym);
duke@1 1273 }
duke@1 1274 e = e.next();
duke@1 1275 }
duke@1 1276 Type st = types.supertype(c.type);
duke@1 1277 if (st != null && st.tag == CLASS) {
duke@1 1278 sym = findMemberType(env, site, name, st.tsym);
duke@1 1279 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1280 }
duke@1 1281 for (List<Type> l = types.interfaces(c.type);
duke@1 1282 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 1283 l = l.tail) {
duke@1 1284 sym = findMemberType(env, site, name, l.head.tsym);
duke@1 1285 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 1286 sym.owner != bestSoFar.owner)
duke@1 1287 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 1288 else if (sym.kind < bestSoFar.kind)
duke@1 1289 bestSoFar = sym;
duke@1 1290 }
duke@1 1291 return bestSoFar;
duke@1 1292 }
duke@1 1293
duke@1 1294 /** Find a global type in given scope and load corresponding class.
duke@1 1295 * @param env The current environment.
duke@1 1296 * @param scope The scope in which to look for the type.
duke@1 1297 * @param name The type's name.
duke@1 1298 */
duke@1 1299 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
duke@1 1300 Symbol bestSoFar = typeNotFound;
duke@1 1301 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
duke@1 1302 Symbol sym = loadClass(env, e.sym.flatName());
duke@1 1303 if (bestSoFar.kind == TYP && sym.kind == TYP &&
duke@1 1304 bestSoFar != sym)
duke@1 1305 return new AmbiguityError(bestSoFar, sym);
duke@1 1306 else if (sym.kind < bestSoFar.kind)
duke@1 1307 bestSoFar = sym;
duke@1 1308 }
duke@1 1309 return bestSoFar;
duke@1 1310 }
duke@1 1311
duke@1 1312 /** Find an unqualified type symbol.
duke@1 1313 * @param env The current environment.
duke@1 1314 * @param name The type's name.
duke@1 1315 */
duke@1 1316 Symbol findType(Env<AttrContext> env, Name name) {
duke@1 1317 Symbol bestSoFar = typeNotFound;
duke@1 1318 Symbol sym;
duke@1 1319 boolean staticOnly = false;
duke@1 1320 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
duke@1 1321 if (isStatic(env1)) staticOnly = true;
duke@1 1322 for (Scope.Entry e = env1.info.scope.lookup(name);
duke@1 1323 e.scope != null;
duke@1 1324 e = e.next()) {
duke@1 1325 if (e.sym.kind == TYP) {
duke@1 1326 if (staticOnly &&
duke@1 1327 e.sym.type.tag == TYPEVAR &&
duke@1 1328 e.sym.owner.kind == TYP) return new StaticError(e.sym);
duke@1 1329 return e.sym;
duke@1 1330 }
duke@1 1331 }
duke@1 1332
duke@1 1333 sym = findMemberType(env1, env1.enclClass.sym.type, name,
duke@1 1334 env1.enclClass.sym);
duke@1 1335 if (staticOnly && sym.kind == TYP &&
duke@1 1336 sym.type.tag == CLASS &&
duke@1 1337 sym.type.getEnclosingType().tag == CLASS &&
duke@1 1338 env1.enclClass.sym.type.isParameterized() &&
duke@1 1339 sym.type.getEnclosingType().isParameterized())
duke@1 1340 return new StaticError(sym);
duke@1 1341 else if (sym.exists()) return sym;
duke@1 1342 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1343
duke@1 1344 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
duke@1 1345 if ((encl.sym.flags() & STATIC) != 0)
duke@1 1346 staticOnly = true;
duke@1 1347 }
duke@1 1348
jjg@1127 1349 if (!env.tree.hasTag(IMPORT)) {
duke@1 1350 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
duke@1 1351 if (sym.exists()) return sym;
duke@1 1352 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1353
duke@1 1354 sym = findGlobalType(env, env.toplevel.packge.members(), name);
duke@1 1355 if (sym.exists()) return sym;
duke@1 1356 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1357
duke@1 1358 sym = findGlobalType(env, env.toplevel.starImportScope, name);
duke@1 1359 if (sym.exists()) return sym;
duke@1 1360 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1361 }
duke@1 1362
duke@1 1363 return bestSoFar;
duke@1 1364 }
duke@1 1365
duke@1 1366 /** Find an unqualified identifier which matches a specified kind set.
duke@1 1367 * @param env The current environment.
duke@1 1368 * @param name The indentifier's name.
duke@1 1369 * @param kind Indicates the possible symbol kinds
duke@1 1370 * (a subset of VAL, TYP, PCK).
duke@1 1371 */
duke@1 1372 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
duke@1 1373 Symbol bestSoFar = typeNotFound;
duke@1 1374 Symbol sym;
duke@1 1375
duke@1 1376 if ((kind & VAR) != 0) {
duke@1 1377 sym = findVar(env, name);
duke@1 1378 if (sym.exists()) return sym;
duke@1 1379 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1380 }
duke@1 1381
duke@1 1382 if ((kind & TYP) != 0) {
duke@1 1383 sym = findType(env, name);
duke@1 1384 if (sym.exists()) return sym;
duke@1 1385 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1386 }
duke@1 1387
duke@1 1388 if ((kind & PCK) != 0) return reader.enterPackage(name);
duke@1 1389 else return bestSoFar;
duke@1 1390 }
duke@1 1391
duke@1 1392 /** Find an identifier in a package which matches a specified kind set.
duke@1 1393 * @param env The current environment.
duke@1 1394 * @param name The identifier's name.
duke@1 1395 * @param kind Indicates the possible symbol kinds
duke@1 1396 * (a nonempty subset of TYP, PCK).
duke@1 1397 */
duke@1 1398 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
duke@1 1399 Name name, int kind) {
duke@1 1400 Name fullname = TypeSymbol.formFullName(name, pck);
duke@1 1401 Symbol bestSoFar = typeNotFound;
duke@1 1402 PackageSymbol pack = null;
duke@1 1403 if ((kind & PCK) != 0) {
duke@1 1404 pack = reader.enterPackage(fullname);
duke@1 1405 if (pack.exists()) return pack;
duke@1 1406 }
duke@1 1407 if ((kind & TYP) != 0) {
duke@1 1408 Symbol sym = loadClass(env, fullname);
duke@1 1409 if (sym.exists()) {
duke@1 1410 // don't allow programs to use flatnames
duke@1 1411 if (name == sym.name) return sym;
duke@1 1412 }
duke@1 1413 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1414 }
duke@1 1415 return (pack != null) ? pack : bestSoFar;
duke@1 1416 }
duke@1 1417
duke@1 1418 /** Find an identifier among the members of a given type `site'.
duke@1 1419 * @param env The current environment.
duke@1 1420 * @param site The type containing the symbol to be found.
duke@1 1421 * @param name The identifier's name.
duke@1 1422 * @param kind Indicates the possible symbol kinds
duke@1 1423 * (a subset of VAL, TYP).
duke@1 1424 */
duke@1 1425 Symbol findIdentInType(Env<AttrContext> env, Type site,
duke@1 1426 Name name, int kind) {
duke@1 1427 Symbol bestSoFar = typeNotFound;
duke@1 1428 Symbol sym;
duke@1 1429 if ((kind & VAR) != 0) {
duke@1 1430 sym = findField(env, site, name, site.tsym);
duke@1 1431 if (sym.exists()) return sym;
duke@1 1432 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1433 }
duke@1 1434
duke@1 1435 if ((kind & TYP) != 0) {
duke@1 1436 sym = findMemberType(env, site, name, site.tsym);
duke@1 1437 if (sym.exists()) return sym;
duke@1 1438 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1439 }
duke@1 1440 return bestSoFar;
duke@1 1441 }
duke@1 1442
duke@1 1443 /* ***************************************************************************
duke@1 1444 * Access checking
duke@1 1445 * The following methods convert ResolveErrors to ErrorSymbols, issuing
duke@1 1446 * an error message in the process
duke@1 1447 ****************************************************************************/
duke@1 1448
duke@1 1449 /** If `sym' is a bad symbol: report error and return errSymbol
duke@1 1450 * else pass through unchanged,
duke@1 1451 * additional arguments duplicate what has been used in trying to find the
duke@1 1452 * symbol (--> flyweight pattern). This improves performance since we
duke@1 1453 * expect misses to happen frequently.
duke@1 1454 *
duke@1 1455 * @param sym The symbol that was found, or a ResolveError.
duke@1 1456 * @param pos The position to use for error reporting.
duke@1 1457 * @param site The original type from where the selection took place.
duke@1 1458 * @param name The symbol's name.
duke@1 1459 * @param argtypes The invocation's value arguments,
duke@1 1460 * if we looked for a method.
duke@1 1461 * @param typeargtypes The invocation's type arguments,
duke@1 1462 * if we looked for a method.
duke@1 1463 */
duke@1 1464 Symbol access(Symbol sym,
duke@1 1465 DiagnosticPosition pos,
mcimadamore@829 1466 Symbol location,
duke@1 1467 Type site,
duke@1 1468 Name name,
duke@1 1469 boolean qualified,
duke@1 1470 List<Type> argtypes,
duke@1 1471 List<Type> typeargtypes) {
duke@1 1472 if (sym.kind >= AMBIGUOUS) {
mcimadamore@302 1473 ResolveError errSym = (ResolveError)sym;
duke@1 1474 if (!site.isErroneous() &&
duke@1 1475 !Type.isErroneous(argtypes) &&
duke@1 1476 (typeargtypes==null || !Type.isErroneous(typeargtypes)))
mcimadamore@829 1477 logResolveError(errSym, pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302 1478 sym = errSym.access(name, qualified ? site.tsym : syms.noSymbol);
duke@1 1479 }
duke@1 1480 return sym;
duke@1 1481 }
duke@1 1482
mcimadamore@829 1483 /** Same as original access(), but without location.
mcimadamore@829 1484 */
mcimadamore@829 1485 Symbol access(Symbol sym,
mcimadamore@829 1486 DiagnosticPosition pos,
mcimadamore@829 1487 Type site,
mcimadamore@829 1488 Name name,
mcimadamore@829 1489 boolean qualified,
mcimadamore@829 1490 List<Type> argtypes,
mcimadamore@829 1491 List<Type> typeargtypes) {
mcimadamore@829 1492 return access(sym, pos, site.tsym, site, name, qualified, argtypes, typeargtypes);
mcimadamore@829 1493 }
mcimadamore@829 1494
mcimadamore@829 1495 /** Same as original access(), but without type arguments and arguments.
mcimadamore@829 1496 */
mcimadamore@829 1497 Symbol access(Symbol sym,
mcimadamore@829 1498 DiagnosticPosition pos,
mcimadamore@829 1499 Symbol location,
mcimadamore@829 1500 Type site,
mcimadamore@829 1501 Name name,
mcimadamore@829 1502 boolean qualified) {
mcimadamore@829 1503 if (sym.kind >= AMBIGUOUS)
mcimadamore@829 1504 return access(sym, pos, location, site, name, qualified, List.<Type>nil(), null);
mcimadamore@829 1505 else
mcimadamore@829 1506 return sym;
mcimadamore@829 1507 }
mcimadamore@829 1508
mcimadamore@829 1509 /** Same as original access(), but without location, type arguments and arguments.
duke@1 1510 */
duke@1 1511 Symbol access(Symbol sym,
duke@1 1512 DiagnosticPosition pos,
duke@1 1513 Type site,
duke@1 1514 Name name,
duke@1 1515 boolean qualified) {
mcimadamore@829 1516 return access(sym, pos, site.tsym, site, name, qualified);
duke@1 1517 }
duke@1 1518
duke@1 1519 /** Check that sym is not an abstract method.
duke@1 1520 */
duke@1 1521 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
duke@1 1522 if ((sym.flags() & ABSTRACT) != 0)
duke@1 1523 log.error(pos, "abstract.cant.be.accessed.directly",
duke@1 1524 kindName(sym), sym, sym.location());
duke@1 1525 }
duke@1 1526
duke@1 1527 /* ***************************************************************************
duke@1 1528 * Debugging
duke@1 1529 ****************************************************************************/
duke@1 1530
duke@1 1531 /** print all scopes starting with scope s and proceeding outwards.
duke@1 1532 * used for debugging.
duke@1 1533 */
duke@1 1534 public void printscopes(Scope s) {
duke@1 1535 while (s != null) {
duke@1 1536 if (s.owner != null)
duke@1 1537 System.err.print(s.owner + ": ");
duke@1 1538 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
duke@1 1539 if ((e.sym.flags() & ABSTRACT) != 0)
duke@1 1540 System.err.print("abstract ");
duke@1 1541 System.err.print(e.sym + " ");
duke@1 1542 }
duke@1 1543 System.err.println();
duke@1 1544 s = s.next;
duke@1 1545 }
duke@1 1546 }
duke@1 1547
duke@1 1548 void printscopes(Env<AttrContext> env) {
duke@1 1549 while (env.outer != null) {
duke@1 1550 System.err.println("------------------------------");
duke@1 1551 printscopes(env.info.scope);
duke@1 1552 env = env.outer;
duke@1 1553 }
duke@1 1554 }
duke@1 1555
duke@1 1556 public void printscopes(Type t) {
duke@1 1557 while (t.tag == CLASS) {
duke@1 1558 printscopes(t.tsym.members());
duke@1 1559 t = types.supertype(t);
duke@1 1560 }
duke@1 1561 }
duke@1 1562
duke@1 1563 /* ***************************************************************************
duke@1 1564 * Name resolution
duke@1 1565 * Naming conventions are as for symbol lookup
duke@1 1566 * Unlike the find... methods these methods will report access errors
duke@1 1567 ****************************************************************************/
duke@1 1568
duke@1 1569 /** Resolve an unqualified (non-method) identifier.
duke@1 1570 * @param pos The position to use for error reporting.
duke@1 1571 * @param env The environment current at the identifier use.
duke@1 1572 * @param name The identifier's name.
duke@1 1573 * @param kind The set of admissible symbol kinds for the identifier.
duke@1 1574 */
duke@1 1575 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1576 Name name, int kind) {
duke@1 1577 return access(
duke@1 1578 findIdent(env, name, kind),
duke@1 1579 pos, env.enclClass.sym.type, name, false);
duke@1 1580 }
duke@1 1581
duke@1 1582 /** Resolve an unqualified method identifier.
duke@1 1583 * @param pos The position to use for error reporting.
duke@1 1584 * @param env The environment current at the method invocation.
duke@1 1585 * @param name The identifier's name.
duke@1 1586 * @param argtypes The types of the invocation's value arguments.
duke@1 1587 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1588 */
duke@1 1589 Symbol resolveMethod(DiagnosticPosition pos,
duke@1 1590 Env<AttrContext> env,
duke@1 1591 Name name,
duke@1 1592 List<Type> argtypes,
duke@1 1593 List<Type> typeargtypes) {
mcimadamore@689 1594 Symbol sym = startResolution();
mcimadamore@160 1595 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1596 while (steps.nonEmpty() &&
mcimadamore@160 1597 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1598 sym.kind >= ERRONEOUS) {
mcimadamore@689 1599 currentStep = steps.head;
mcimadamore@160 1600 sym = findFun(env, name, argtypes, typeargtypes,
mcimadamore@160 1601 steps.head.isBoxingRequired,
mcimadamore@160 1602 env.info.varArgs = steps.head.isVarargsRequired);
mcimadamore@160 1603 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1604 steps = steps.tail;
duke@1 1605 }
mcimadamore@160 1606 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160 1607 MethodResolutionPhase errPhase =
mcimadamore@160 1608 firstErroneousResolutionPhase();
mcimadamore@160 1609 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160 1610 pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
mcimadamore@160 1611 env.info.varArgs = errPhase.isVarargsRequired;
duke@1 1612 }
duke@1 1613 return sym;
duke@1 1614 }
duke@1 1615
mcimadamore@689 1616 private Symbol startResolution() {
mcimadamore@689 1617 wrongMethod.clear();
mcimadamore@689 1618 wrongMethods.clear();
mcimadamore@689 1619 return methodNotFound;
mcimadamore@689 1620 }
mcimadamore@689 1621
duke@1 1622 /** Resolve a qualified method identifier
duke@1 1623 * @param pos The position to use for error reporting.
duke@1 1624 * @param env The environment current at the method invocation.
duke@1 1625 * @param site The type of the qualifying expression, in which
duke@1 1626 * identifier is searched.
duke@1 1627 * @param name The identifier's name.
duke@1 1628 * @param argtypes The types of the invocation's value arguments.
duke@1 1629 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1630 */
duke@1 1631 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1632 Type site, Name name, List<Type> argtypes,
duke@1 1633 List<Type> typeargtypes) {
mcimadamore@829 1634 return resolveQualifiedMethod(pos, env, site.tsym, site, name, argtypes, typeargtypes);
mcimadamore@829 1635 }
mcimadamore@829 1636 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
mcimadamore@829 1637 Symbol location, Type site, Name name, List<Type> argtypes,
mcimadamore@829 1638 List<Type> typeargtypes) {
mcimadamore@689 1639 Symbol sym = startResolution();
mcimadamore@160 1640 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1641 while (steps.nonEmpty() &&
mcimadamore@160 1642 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1643 sym.kind >= ERRONEOUS) {
mcimadamore@689 1644 currentStep = steps.head;
mcimadamore@160 1645 sym = findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@160 1646 steps.head.isBoxingRequired(),
mcimadamore@160 1647 env.info.varArgs = steps.head.isVarargsRequired(), false);
mcimadamore@160 1648 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1649 steps = steps.tail;
duke@1 1650 }
mcimadamore@674 1651 if (sym.kind >= AMBIGUOUS) {
mcimadamore@820 1652 if (site.tsym.isPolymorphicSignatureGeneric()) {
mcimadamore@674 1653 //polymorphic receiver - synthesize new method symbol
mcimadamore@674 1654 env.info.varArgs = false;
mcimadamore@674 1655 sym = findPolymorphicSignatureInstance(env,
mcimadamore@820 1656 site, name, null, argtypes);
mcimadamore@674 1657 }
mcimadamore@674 1658 else {
mcimadamore@674 1659 //if nothing is found return the 'first' error
mcimadamore@674 1660 MethodResolutionPhase errPhase =
mcimadamore@674 1661 firstErroneousResolutionPhase();
mcimadamore@674 1662 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@829 1663 pos, location, site, name, true, argtypes, typeargtypes);
mcimadamore@674 1664 env.info.varArgs = errPhase.isVarargsRequired;
mcimadamore@674 1665 }
mcimadamore@674 1666 } else if (allowMethodHandles && sym.isPolymorphicSignatureGeneric()) {
mcimadamore@674 1667 //non-instantiated polymorphic signature - synthesize new method symbol
mcimadamore@674 1668 env.info.varArgs = false;
mcimadamore@674 1669 sym = findPolymorphicSignatureInstance(env,
mcimadamore@820 1670 site, name, (MethodSymbol)sym, argtypes);
duke@1 1671 }
duke@1 1672 return sym;
duke@1 1673 }
duke@1 1674
mcimadamore@674 1675 /** Find or create an implicit method of exactly the given type (after erasure).
mcimadamore@674 1676 * Searches in a side table, not the main scope of the site.
mcimadamore@674 1677 * This emulates the lookup process required by JSR 292 in JVM.
mcimadamore@674 1678 * @param env Attribution environment
mcimadamore@674 1679 * @param site The original type from where the selection takes place.
mcimadamore@674 1680 * @param name The method's name.
mcimadamore@674 1681 * @param spMethod A template for the implicit method, or null.
mcimadamore@674 1682 * @param argtypes The required argument types.
mcimadamore@674 1683 * @param typeargtypes The required type arguments.
mcimadamore@674 1684 */
mcimadamore@674 1685 Symbol findPolymorphicSignatureInstance(Env<AttrContext> env, Type site,
mcimadamore@674 1686 Name name,
mcimadamore@674 1687 MethodSymbol spMethod, // sig. poly. method or null if none
mcimadamore@820 1688 List<Type> argtypes) {
mcimadamore@674 1689 Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
mcimadamore@820 1690 site, name, spMethod, argtypes);
mcimadamore@674 1691 long flags = ABSTRACT | HYPOTHETICAL | POLYMORPHIC_SIGNATURE |
mcimadamore@674 1692 (spMethod != null ?
mcimadamore@674 1693 spMethod.flags() & Flags.AccessFlags :
mcimadamore@674 1694 Flags.PUBLIC | Flags.STATIC);
mcimadamore@674 1695 Symbol m = null;
mcimadamore@674 1696 for (Scope.Entry e = polymorphicSignatureScope.lookup(name);
mcimadamore@674 1697 e.scope != null;
mcimadamore@674 1698 e = e.next()) {
mcimadamore@674 1699 Symbol sym = e.sym;
mcimadamore@674 1700 if (types.isSameType(mtype, sym.type) &&
mcimadamore@674 1701 (sym.flags() & Flags.STATIC) == (flags & Flags.STATIC) &&
mcimadamore@674 1702 types.isSameType(sym.owner.type, site)) {
mcimadamore@674 1703 m = sym;
mcimadamore@674 1704 break;
mcimadamore@674 1705 }
mcimadamore@674 1706 }
mcimadamore@674 1707 if (m == null) {
mcimadamore@674 1708 // create the desired method
mcimadamore@674 1709 m = new MethodSymbol(flags, name, mtype, site.tsym);
mcimadamore@674 1710 polymorphicSignatureScope.enter(m);
mcimadamore@674 1711 }
mcimadamore@674 1712 return m;
mcimadamore@674 1713 }
mcimadamore@674 1714
duke@1 1715 /** Resolve a qualified method identifier, throw a fatal error if not
duke@1 1716 * found.
duke@1 1717 * @param pos The position to use for error reporting.
duke@1 1718 * @param env The environment current at the method invocation.
duke@1 1719 * @param site The type of the qualifying expression, in which
duke@1 1720 * identifier is searched.
duke@1 1721 * @param name The identifier's name.
duke@1 1722 * @param argtypes The types of the invocation's value arguments.
duke@1 1723 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1724 */
duke@1 1725 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1726 Type site, Name name,
duke@1 1727 List<Type> argtypes,
duke@1 1728 List<Type> typeargtypes) {
mcimadamore@1114 1729 boolean prevInternal = internalResolution;
mcimadamore@1114 1730 try {
mcimadamore@1114 1731 internalResolution = true;
mcimadamore@1114 1732 Symbol sym = resolveQualifiedMethod(
mcimadamore@1114 1733 pos, env, site.tsym, site, name, argtypes, typeargtypes);
mcimadamore@1114 1734 if (sym.kind == MTH) return (MethodSymbol)sym;
mcimadamore@1114 1735 else throw new FatalError(
mcimadamore@1114 1736 diags.fragment("fatal.err.cant.locate.meth",
mcimadamore@1114 1737 name));
mcimadamore@1114 1738 }
mcimadamore@1114 1739 finally {
mcimadamore@1114 1740 internalResolution = prevInternal;
mcimadamore@1114 1741 }
duke@1 1742 }
duke@1 1743
duke@1 1744 /** Resolve constructor.
duke@1 1745 * @param pos The position to use for error reporting.
duke@1 1746 * @param env The environment current at the constructor invocation.
duke@1 1747 * @param site The type of class for which a constructor is searched.
duke@1 1748 * @param argtypes The types of the constructor invocation's value
duke@1 1749 * arguments.
duke@1 1750 * @param typeargtypes The types of the constructor invocation's type
duke@1 1751 * arguments.
duke@1 1752 */
duke@1 1753 Symbol resolveConstructor(DiagnosticPosition pos,
duke@1 1754 Env<AttrContext> env,
duke@1 1755 Type site,
duke@1 1756 List<Type> argtypes,
duke@1 1757 List<Type> typeargtypes) {
mcimadamore@689 1758 Symbol sym = startResolution();
mcimadamore@160 1759 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1760 while (steps.nonEmpty() &&
mcimadamore@160 1761 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1762 sym.kind >= ERRONEOUS) {
mcimadamore@689 1763 currentStep = steps.head;
mcimadamore@160 1764 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@160 1765 steps.head.isBoxingRequired(),
mcimadamore@160 1766 env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@160 1767 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1768 steps = steps.tail;
duke@1 1769 }
mcimadamore@160 1770 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160 1771 MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
mcimadamore@160 1772 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160 1773 pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@160 1774 env.info.varArgs = errPhase.isVarargsRequired();
duke@1 1775 }
duke@1 1776 return sym;
duke@1 1777 }
duke@1 1778
mcimadamore@537 1779 /** Resolve constructor using diamond inference.
mcimadamore@537 1780 * @param pos The position to use for error reporting.
mcimadamore@537 1781 * @param env The environment current at the constructor invocation.
mcimadamore@537 1782 * @param site The type of class for which a constructor is searched.
mcimadamore@537 1783 * The scope of this class has been touched in attribution.
mcimadamore@537 1784 * @param argtypes The types of the constructor invocation's value
mcimadamore@537 1785 * arguments.
mcimadamore@537 1786 * @param typeargtypes The types of the constructor invocation's type
mcimadamore@537 1787 * arguments.
mcimadamore@537 1788 */
mcimadamore@537 1789 Symbol resolveDiamond(DiagnosticPosition pos,
mcimadamore@537 1790 Env<AttrContext> env,
mcimadamore@537 1791 Type site,
mcimadamore@537 1792 List<Type> argtypes,
mcimadamore@631 1793 List<Type> typeargtypes) {
mcimadamore@689 1794 Symbol sym = startResolution();
mcimadamore@537 1795 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@537 1796 while (steps.nonEmpty() &&
mcimadamore@537 1797 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@537 1798 sym.kind >= ERRONEOUS) {
mcimadamore@689 1799 currentStep = steps.head;
mcimadamore@537 1800 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@537 1801 steps.head.isBoxingRequired(),
mcimadamore@537 1802 env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@537 1803 methodResolutionCache.put(steps.head, sym);
mcimadamore@537 1804 steps = steps.tail;
mcimadamore@537 1805 }
mcimadamore@631 1806 if (sym.kind >= AMBIGUOUS) {
mcimadamore@689 1807 final JCDiagnostic details = sym.kind == WRONG_MTH ?
mcimadamore@689 1808 ((InapplicableSymbolError)sym).explanation :
mcimadamore@689 1809 null;
mcimadamore@631 1810 Symbol errSym = new ResolveError(WRONG_MTH, "diamond error") {
mcimadamore@631 1811 @Override
mcimadamore@829 1812 JCDiagnostic getDiagnostic(DiagnosticType dkind, DiagnosticPosition pos,
mcimadamore@829 1813 Symbol location, Type site, Name name, List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@631 1814 String key = details == null ?
mcimadamore@631 1815 "cant.apply.diamond" :
mcimadamore@631 1816 "cant.apply.diamond.1";
mcimadamore@829 1817 return diags.create(dkind, log.currentSource(), pos, key,
mcimadamore@829 1818 diags.fragment("diamond", site.tsym), details);
mcimadamore@631 1819 }
mcimadamore@631 1820 };
mcimadamore@631 1821 MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
mcimadamore@631 1822 sym = access(errSym, pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@631 1823 env.info.varArgs = errPhase.isVarargsRequired();
mcimadamore@537 1824 }
mcimadamore@537 1825 return sym;
mcimadamore@537 1826 }
mcimadamore@537 1827
duke@1 1828 /** Resolve constructor.
duke@1 1829 * @param pos The position to use for error reporting.
duke@1 1830 * @param env The environment current at the constructor invocation.
duke@1 1831 * @param site The type of class for which a constructor is searched.
duke@1 1832 * @param argtypes The types of the constructor invocation's value
duke@1 1833 * arguments.
duke@1 1834 * @param typeargtypes The types of the constructor invocation's type
duke@1 1835 * arguments.
duke@1 1836 * @param allowBoxing Allow boxing and varargs conversions.
duke@1 1837 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1838 */
duke@1 1839 Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1840 Type site, List<Type> argtypes,
duke@1 1841 List<Type> typeargtypes,
duke@1 1842 boolean allowBoxing,
duke@1 1843 boolean useVarargs) {
duke@1 1844 Symbol sym = findMethod(env, site,
duke@1 1845 names.init, argtypes,
duke@1 1846 typeargtypes, allowBoxing,
duke@1 1847 useVarargs, false);
mcimadamore@852 1848 chk.checkDeprecated(pos, env.info.scope.owner, sym);
duke@1 1849 return sym;
duke@1 1850 }
duke@1 1851
duke@1 1852 /** Resolve a constructor, throw a fatal error if not found.
duke@1 1853 * @param pos The position to use for error reporting.
duke@1 1854 * @param env The environment current at the method invocation.
duke@1 1855 * @param site The type to be constructed.
duke@1 1856 * @param argtypes The types of the invocation's value arguments.
duke@1 1857 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1858 */
duke@1 1859 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1860 Type site,
duke@1 1861 List<Type> argtypes,
duke@1 1862 List<Type> typeargtypes) {
duke@1 1863 Symbol sym = resolveConstructor(
duke@1 1864 pos, env, site, argtypes, typeargtypes);
duke@1 1865 if (sym.kind == MTH) return (MethodSymbol)sym;
duke@1 1866 else throw new FatalError(
mcimadamore@89 1867 diags.fragment("fatal.err.cant.locate.ctor", site));
duke@1 1868 }
duke@1 1869
duke@1 1870 /** Resolve operator.
duke@1 1871 * @param pos The position to use for error reporting.
duke@1 1872 * @param optag The tag of the operation tree.
duke@1 1873 * @param env The environment current at the operation.
duke@1 1874 * @param argtypes The types of the operands.
duke@1 1875 */
jjg@1127 1876 Symbol resolveOperator(DiagnosticPosition pos, JCTree.Tag optag,
duke@1 1877 Env<AttrContext> env, List<Type> argtypes) {
mcimadamore@1110 1878 startResolution();
duke@1 1879 Name name = treeinfo.operatorName(optag);
duke@1 1880 Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1881 null, false, false, true);
duke@1 1882 if (boxingEnabled && sym.kind >= WRONG_MTHS)
duke@1 1883 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1884 null, true, false, true);
duke@1 1885 return access(sym, pos, env.enclClass.sym.type, name,
duke@1 1886 false, argtypes, null);
duke@1 1887 }
duke@1 1888
duke@1 1889 /** Resolve operator.
duke@1 1890 * @param pos The position to use for error reporting.
duke@1 1891 * @param optag The tag of the operation tree.
duke@1 1892 * @param env The environment current at the operation.
duke@1 1893 * @param arg The type of the operand.
duke@1 1894 */
jjg@1127 1895 Symbol resolveUnaryOperator(DiagnosticPosition pos, JCTree.Tag optag, Env<AttrContext> env, Type arg) {
duke@1 1896 return resolveOperator(pos, optag, env, List.of(arg));
duke@1 1897 }
duke@1 1898
duke@1 1899 /** Resolve binary operator.
duke@1 1900 * @param pos The position to use for error reporting.
duke@1 1901 * @param optag The tag of the operation tree.
duke@1 1902 * @param env The environment current at the operation.
duke@1 1903 * @param left The types of the left operand.
duke@1 1904 * @param right The types of the right operand.
duke@1 1905 */
duke@1 1906 Symbol resolveBinaryOperator(DiagnosticPosition pos,
jjg@1127 1907 JCTree.Tag optag,
duke@1 1908 Env<AttrContext> env,
duke@1 1909 Type left,
duke@1 1910 Type right) {
duke@1 1911 return resolveOperator(pos, optag, env, List.of(left, right));
duke@1 1912 }
duke@1 1913
duke@1 1914 /**
duke@1 1915 * Resolve `c.name' where name == this or name == super.
duke@1 1916 * @param pos The position to use for error reporting.
duke@1 1917 * @param env The environment current at the expression.
duke@1 1918 * @param c The qualifier.
duke@1 1919 * @param name The identifier's name.
duke@1 1920 */
duke@1 1921 Symbol resolveSelf(DiagnosticPosition pos,
duke@1 1922 Env<AttrContext> env,
duke@1 1923 TypeSymbol c,
duke@1 1924 Name name) {
duke@1 1925 Env<AttrContext> env1 = env;
duke@1 1926 boolean staticOnly = false;
duke@1 1927 while (env1.outer != null) {
duke@1 1928 if (isStatic(env1)) staticOnly = true;
duke@1 1929 if (env1.enclClass.sym == c) {
duke@1 1930 Symbol sym = env1.info.scope.lookup(name).sym;
duke@1 1931 if (sym != null) {
duke@1 1932 if (staticOnly) sym = new StaticError(sym);
duke@1 1933 return access(sym, pos, env.enclClass.sym.type,
duke@1 1934 name, true);
duke@1 1935 }
duke@1 1936 }
duke@1 1937 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 1938 env1 = env1.outer;
duke@1 1939 }
duke@1 1940 log.error(pos, "not.encl.class", c);
duke@1 1941 return syms.errSymbol;
duke@1 1942 }
duke@1 1943
duke@1 1944 /**
duke@1 1945 * Resolve `c.this' for an enclosing class c that contains the
duke@1 1946 * named member.
duke@1 1947 * @param pos The position to use for error reporting.
duke@1 1948 * @param env The environment current at the expression.
duke@1 1949 * @param member The member that must be contained in the result.
duke@1 1950 */
duke@1 1951 Symbol resolveSelfContaining(DiagnosticPosition pos,
duke@1 1952 Env<AttrContext> env,
mcimadamore@901 1953 Symbol member,
mcimadamore@901 1954 boolean isSuperCall) {
duke@1 1955 Name name = names._this;
mcimadamore@901 1956 Env<AttrContext> env1 = isSuperCall ? env.outer : env;
duke@1 1957 boolean staticOnly = false;
mcimadamore@901 1958 if (env1 != null) {
mcimadamore@901 1959 while (env1 != null && env1.outer != null) {
mcimadamore@901 1960 if (isStatic(env1)) staticOnly = true;
mcimadamore@901 1961 if (env1.enclClass.sym.isSubClass(member.owner, types)) {
mcimadamore@901 1962 Symbol sym = env1.info.scope.lookup(name).sym;
mcimadamore@901 1963 if (sym != null) {
mcimadamore@901 1964 if (staticOnly) sym = new StaticError(sym);
mcimadamore@901 1965 return access(sym, pos, env.enclClass.sym.type,
mcimadamore@901 1966 name, true);
mcimadamore@901 1967 }
duke@1 1968 }
mcimadamore@901 1969 if ((env1.enclClass.sym.flags() & STATIC) != 0)
mcimadamore@901 1970 staticOnly = true;
mcimadamore@901 1971 env1 = env1.outer;
duke@1 1972 }
duke@1 1973 }
duke@1 1974 log.error(pos, "encl.class.required", member);
duke@1 1975 return syms.errSymbol;
duke@1 1976 }
duke@1 1977
duke@1 1978 /**
duke@1 1979 * Resolve an appropriate implicit this instance for t's container.
jjh@972 1980 * JLS 8.8.5.1 and 15.9.2
duke@1 1981 */
duke@1 1982 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
mcimadamore@901 1983 return resolveImplicitThis(pos, env, t, false);
mcimadamore@901 1984 }
mcimadamore@901 1985
mcimadamore@901 1986 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t, boolean isSuperCall) {
duke@1 1987 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
duke@1 1988 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
mcimadamore@901 1989 : resolveSelfContaining(pos, env, t.tsym, isSuperCall)).type;
duke@1 1990 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
duke@1 1991 log.error(pos, "cant.ref.before.ctor.called", "this");
duke@1 1992 return thisType;
duke@1 1993 }
duke@1 1994
duke@1 1995 /* ***************************************************************************
duke@1 1996 * ResolveError classes, indicating error situations when accessing symbols
duke@1 1997 ****************************************************************************/
duke@1 1998
duke@1 1999 public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
duke@1 2000 AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
mcimadamore@829 2001 logResolveError(error, tree.pos(), type.getEnclosingType().tsym, type.getEnclosingType(), null, null, null);
mcimadamore@302 2002 }
mcimadamore@302 2003 //where
mcimadamore@302 2004 private void logResolveError(ResolveError error,
mcimadamore@302 2005 DiagnosticPosition pos,
mcimadamore@829 2006 Symbol location,
mcimadamore@302 2007 Type site,
mcimadamore@302 2008 Name name,
mcimadamore@302 2009 List<Type> argtypes,
mcimadamore@302 2010 List<Type> typeargtypes) {
mcimadamore@302 2011 JCDiagnostic d = error.getDiagnostic(JCDiagnostic.DiagnosticType.ERROR,
mcimadamore@829 2012 pos, location, site, name, argtypes, typeargtypes);
jjg@643 2013 if (d != null) {
jjg@643 2014 d.setFlag(DiagnosticFlag.RESOLVE_ERROR);
mcimadamore@302 2015 log.report(d);
jjg@643 2016 }
duke@1 2017 }
duke@1 2018
mcimadamore@161 2019 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
mcimadamore@161 2020
mcimadamore@161 2021 public Object methodArguments(List<Type> argtypes) {
mcimadamore@1114 2022 return argtypes == null || argtypes.isEmpty() ? noArgs : argtypes;
mcimadamore@161 2023 }
mcimadamore@161 2024
mcimadamore@302 2025 /**
mcimadamore@302 2026 * Root class for resolution errors. Subclass of ResolveError
mcimadamore@302 2027 * represent a different kinds of resolution error - as such they must
mcimadamore@302 2028 * specify how they map into concrete compiler diagnostics.
duke@1 2029 */
mcimadamore@302 2030 private abstract class ResolveError extends Symbol {
duke@1 2031
mcimadamore@302 2032 /** The name of the kind of error, for debugging only. */
mcimadamore@302 2033 final String debugName;
mcimadamore@302 2034
mcimadamore@302 2035 ResolveError(int kind, String debugName) {
duke@1 2036 super(kind, 0, null, null, null);
duke@1 2037 this.debugName = debugName;
duke@1 2038 }
duke@1 2039
mcimadamore@302 2040 @Override
duke@1 2041 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 2042 throw new AssertionError();
duke@1 2043 }
duke@1 2044
mcimadamore@302 2045 @Override
duke@1 2046 public String toString() {
mcimadamore@302 2047 return debugName;
duke@1 2048 }
duke@1 2049
mcimadamore@302 2050 @Override
mcimadamore@302 2051 public boolean exists() {
mcimadamore@302 2052 return false;
duke@1 2053 }
duke@1 2054
mcimadamore@302 2055 /**
mcimadamore@302 2056 * Create an external representation for this erroneous symbol to be
mcimadamore@302 2057 * used during attribution - by default this returns the symbol of a
mcimadamore@302 2058 * brand new error type which stores the original type found
mcimadamore@302 2059 * during resolution.
mcimadamore@302 2060 *
mcimadamore@302 2061 * @param name the name used during resolution
mcimadamore@302 2062 * @param location the location from which the symbol is accessed
duke@1 2063 */
mcimadamore@302 2064 protected Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2065 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
duke@1 2066 }
duke@1 2067
mcimadamore@302 2068 /**
mcimadamore@302 2069 * Create a diagnostic representing this resolution error.
mcimadamore@302 2070 *
mcimadamore@302 2071 * @param dkind The kind of the diagnostic to be created (e.g error).
mcimadamore@302 2072 * @param pos The position to be used for error reporting.
mcimadamore@302 2073 * @param site The original type from where the selection took place.
mcimadamore@302 2074 * @param name The name of the symbol to be resolved.
mcimadamore@302 2075 * @param argtypes The invocation's value arguments,
mcimadamore@302 2076 * if we looked for a method.
mcimadamore@302 2077 * @param typeargtypes The invocation's type arguments,
mcimadamore@302 2078 * if we looked for a method.
mcimadamore@302 2079 */
mcimadamore@302 2080 abstract JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2081 DiagnosticPosition pos,
mcimadamore@829 2082 Symbol location,
mcimadamore@302 2083 Type site,
mcimadamore@302 2084 Name name,
mcimadamore@302 2085 List<Type> argtypes,
mcimadamore@302 2086 List<Type> typeargtypes);
duke@1 2087
mcimadamore@302 2088 /**
mcimadamore@302 2089 * A name designates an operator if it consists
mcimadamore@302 2090 * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
mcimadamore@80 2091 */
mcimadamore@80 2092 boolean isOperator(Name name) {
mcimadamore@80 2093 int i = 0;
jjg@113 2094 while (i < name.getByteLength() &&
jjg@113 2095 "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++;
jjg@113 2096 return i > 0 && i == name.getByteLength();
mcimadamore@80 2097 }
duke@1 2098 }
duke@1 2099
mcimadamore@302 2100 /**
mcimadamore@302 2101 * This class is the root class of all resolution errors caused by
mcimadamore@302 2102 * an invalid symbol being found during resolution.
duke@1 2103 */
mcimadamore@302 2104 abstract class InvalidSymbolError extends ResolveError {
mcimadamore@302 2105
mcimadamore@302 2106 /** The invalid symbol found during resolution */
mcimadamore@302 2107 Symbol sym;
mcimadamore@302 2108
mcimadamore@302 2109 InvalidSymbolError(int kind, Symbol sym, String debugName) {
mcimadamore@302 2110 super(kind, debugName);
mcimadamore@302 2111 this.sym = sym;
mcimadamore@302 2112 }
mcimadamore@302 2113
mcimadamore@302 2114 @Override
mcimadamore@302 2115 public boolean exists() {
mcimadamore@302 2116 return true;
mcimadamore@302 2117 }
mcimadamore@302 2118
mcimadamore@302 2119 @Override
mcimadamore@302 2120 public String toString() {
mcimadamore@302 2121 return super.toString() + " wrongSym=" + sym;
mcimadamore@302 2122 }
mcimadamore@302 2123
mcimadamore@302 2124 @Override
mcimadamore@302 2125 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2126 if (sym.kind >= AMBIGUOUS)
mcimadamore@302 2127 return ((ResolveError)sym).access(name, location);
mcimadamore@302 2128 else if ((sym.kind & ERRONEOUS) == 0 && (sym.kind & TYP) != 0)
mcimadamore@302 2129 return types.createErrorType(name, location, sym.type).tsym;
mcimadamore@302 2130 else
mcimadamore@302 2131 return sym;
mcimadamore@302 2132 }
mcimadamore@302 2133 }
mcimadamore@302 2134
mcimadamore@302 2135 /**
mcimadamore@302 2136 * InvalidSymbolError error class indicating that a symbol matching a
mcimadamore@302 2137 * given name does not exists in a given site.
mcimadamore@302 2138 */
mcimadamore@302 2139 class SymbolNotFoundError extends ResolveError {
mcimadamore@302 2140
mcimadamore@302 2141 SymbolNotFoundError(int kind) {
mcimadamore@302 2142 super(kind, "symbol not found error");
mcimadamore@302 2143 }
mcimadamore@302 2144
mcimadamore@302 2145 @Override
mcimadamore@302 2146 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2147 DiagnosticPosition pos,
mcimadamore@829 2148 Symbol location,
mcimadamore@302 2149 Type site,
mcimadamore@302 2150 Name name,
mcimadamore@302 2151 List<Type> argtypes,
mcimadamore@302 2152 List<Type> typeargtypes) {
mcimadamore@302 2153 argtypes = argtypes == null ? List.<Type>nil() : argtypes;
mcimadamore@302 2154 typeargtypes = typeargtypes == null ? List.<Type>nil() : typeargtypes;
mcimadamore@302 2155 if (name == names.error)
mcimadamore@302 2156 return null;
mcimadamore@302 2157
mcimadamore@302 2158 if (isOperator(name)) {
mcimadamore@829 2159 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@829 2160 String key = argtypes.size() == 1 ?
mcimadamore@829 2161 "operator.cant.be.applied" :
mcimadamore@829 2162 "operator.cant.be.applied.1";
mcimadamore@829 2163 Type first = argtypes.head;
mcimadamore@829 2164 Type second = !isUnaryOp ? argtypes.tail.head : null;
jjg@612 2165 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 2166 key, name, first, second);
mcimadamore@302 2167 }
mcimadamore@302 2168 boolean hasLocation = false;
mcimadamore@855 2169 if (location == null) {
mcimadamore@855 2170 location = site.tsym;
mcimadamore@855 2171 }
mcimadamore@829 2172 if (!location.name.isEmpty()) {
mcimadamore@829 2173 if (location.kind == PCK && !site.tsym.exists()) {
jjg@612 2174 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@829 2175 "doesnt.exist", location);
mcimadamore@302 2176 }
mcimadamore@829 2177 hasLocation = !location.name.equals(names._this) &&
mcimadamore@829 2178 !location.name.equals(names._super);
mcimadamore@302 2179 }
mcimadamore@302 2180 boolean isConstructor = kind == ABSENT_MTH &&
mcimadamore@302 2181 name == names.table.names.init;
mcimadamore@302 2182 KindName kindname = isConstructor ? KindName.CONSTRUCTOR : absentKind(kind);
mcimadamore@302 2183 Name idname = isConstructor ? site.tsym.name : name;
mcimadamore@302 2184 String errKey = getErrorKey(kindname, typeargtypes.nonEmpty(), hasLocation);
mcimadamore@302 2185 if (hasLocation) {
jjg@612 2186 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2187 errKey, kindname, idname, //symbol kindname, name
mcimadamore@302 2188 typeargtypes, argtypes, //type parameters and arguments (if any)
mcimadamore@855 2189 getLocationDiag(location, site)); //location kindname, type
mcimadamore@302 2190 }
mcimadamore@302 2191 else {
jjg@612 2192 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2193 errKey, kindname, idname, //symbol kindname, name
mcimadamore@302 2194 typeargtypes, argtypes); //type parameters and arguments (if any)
mcimadamore@302 2195 }
mcimadamore@302 2196 }
mcimadamore@302 2197 //where
mcimadamore@302 2198 private String getErrorKey(KindName kindname, boolean hasTypeArgs, boolean hasLocation) {
mcimadamore@302 2199 String key = "cant.resolve";
mcimadamore@302 2200 String suffix = hasLocation ? ".location" : "";
mcimadamore@302 2201 switch (kindname) {
mcimadamore@302 2202 case METHOD:
mcimadamore@302 2203 case CONSTRUCTOR: {
mcimadamore@302 2204 suffix += ".args";
mcimadamore@302 2205 suffix += hasTypeArgs ? ".params" : "";
mcimadamore@302 2206 }
mcimadamore@302 2207 }
mcimadamore@302 2208 return key + suffix;
mcimadamore@302 2209 }
mcimadamore@855 2210 private JCDiagnostic getLocationDiag(Symbol location, Type site) {
mcimadamore@855 2211 if (location.kind == VAR) {
mcimadamore@855 2212 return diags.fragment("location.1",
mcimadamore@829 2213 kindName(location),
mcimadamore@829 2214 location,
mcimadamore@855 2215 location.type);
mcimadamore@855 2216 } else {
mcimadamore@855 2217 return diags.fragment("location",
mcimadamore@855 2218 typeKindName(site),
mcimadamore@855 2219 site,
mcimadamore@855 2220 null);
mcimadamore@855 2221 }
mcimadamore@829 2222 }
mcimadamore@302 2223 }
mcimadamore@302 2224
mcimadamore@302 2225 /**
mcimadamore@302 2226 * InvalidSymbolError error class indicating that a given symbol
mcimadamore@302 2227 * (either a method, a constructor or an operand) is not applicable
mcimadamore@302 2228 * given an actual arguments/type argument list.
mcimadamore@302 2229 */
mcimadamore@302 2230 class InapplicableSymbolError extends InvalidSymbolError {
mcimadamore@302 2231
mcimadamore@302 2232 /** An auxiliary explanation set in case of instantiation errors. */
mcimadamore@302 2233 JCDiagnostic explanation;
mcimadamore@302 2234
mcimadamore@302 2235 InapplicableSymbolError(Symbol sym) {
mcimadamore@302 2236 super(WRONG_MTH, sym, "inapplicable symbol error");
mcimadamore@302 2237 }
mcimadamore@302 2238
mcimadamore@302 2239 /** Update sym and explanation and return this.
mcimadamore@302 2240 */
mcimadamore@302 2241 InapplicableSymbolError setWrongSym(Symbol sym, JCDiagnostic explanation) {
mcimadamore@302 2242 this.sym = sym;
mcimadamore@689 2243 if (this.sym == sym && explanation != null)
mcimadamore@689 2244 this.explanation = explanation; //update the details
mcimadamore@302 2245 return this;
mcimadamore@302 2246 }
mcimadamore@302 2247
mcimadamore@302 2248 /** Update sym and return this.
mcimadamore@302 2249 */
mcimadamore@302 2250 InapplicableSymbolError setWrongSym(Symbol sym) {
mcimadamore@302 2251 this.sym = sym;
mcimadamore@302 2252 return this;
mcimadamore@302 2253 }
mcimadamore@302 2254
mcimadamore@302 2255 @Override
mcimadamore@302 2256 public String toString() {
mcimadamore@302 2257 return super.toString() + " explanation=" + explanation;
mcimadamore@302 2258 }
mcimadamore@302 2259
mcimadamore@302 2260 @Override
mcimadamore@302 2261 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2262 DiagnosticPosition pos,
mcimadamore@829 2263 Symbol location,
mcimadamore@302 2264 Type site,
mcimadamore@302 2265 Name name,
mcimadamore@302 2266 List<Type> argtypes,
mcimadamore@302 2267 List<Type> typeargtypes) {
mcimadamore@302 2268 if (name == names.error)
mcimadamore@302 2269 return null;
mcimadamore@302 2270
mcimadamore@302 2271 if (isOperator(name)) {
mcimadamore@853 2272 boolean isUnaryOp = argtypes.size() == 1;
mcimadamore@853 2273 String key = argtypes.size() == 1 ?
mcimadamore@853 2274 "operator.cant.be.applied" :
mcimadamore@853 2275 "operator.cant.be.applied.1";
mcimadamore@853 2276 Type first = argtypes.head;
mcimadamore@853 2277 Type second = !isUnaryOp ? argtypes.tail.head : null;
mcimadamore@853 2278 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@853 2279 key, name, first, second);
mcimadamore@302 2280 }
mcimadamore@302 2281 else {
mcimadamore@302 2282 Symbol ws = sym.asMemberOf(site, types);
jjg@612 2283 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2284 "cant.apply.symbol" + (explanation != null ? ".1" : ""),
mcimadamore@302 2285 kindName(ws),
mcimadamore@302 2286 ws.name == names.init ? ws.owner.name : ws.name,
mcimadamore@302 2287 methodArguments(ws.type.getParameterTypes()),
mcimadamore@302 2288 methodArguments(argtypes),
mcimadamore@302 2289 kindName(ws.owner),
mcimadamore@302 2290 ws.owner.type,
mcimadamore@302 2291 explanation);
mcimadamore@302 2292 }
mcimadamore@302 2293 }
mcimadamore@302 2294
mcimadamore@689 2295 void clear() {
mcimadamore@689 2296 explanation = null;
mcimadamore@689 2297 }
mcimadamore@689 2298
mcimadamore@302 2299 @Override
mcimadamore@302 2300 public Symbol access(Name name, TypeSymbol location) {
mcimadamore@302 2301 return types.createErrorType(name, location, syms.errSymbol.type).tsym;
mcimadamore@302 2302 }
mcimadamore@302 2303 }
mcimadamore@302 2304
mcimadamore@302 2305 /**
mcimadamore@302 2306 * ResolveError error class indicating that a set of symbols
mcimadamore@302 2307 * (either methods, constructors or operands) is not applicable
mcimadamore@302 2308 * given an actual arguments/type argument list.
mcimadamore@302 2309 */
mcimadamore@302 2310 class InapplicableSymbolsError extends ResolveError {
mcimadamore@689 2311
mcimadamore@689 2312 private List<Candidate> candidates = List.nil();
mcimadamore@689 2313
mcimadamore@302 2314 InapplicableSymbolsError(Symbol sym) {
mcimadamore@302 2315 super(WRONG_MTHS, "inapplicable symbols");
mcimadamore@302 2316 }
mcimadamore@302 2317
mcimadamore@302 2318 @Override
mcimadamore@302 2319 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2320 DiagnosticPosition pos,
mcimadamore@829 2321 Symbol location,
mcimadamore@302 2322 Type site,
mcimadamore@302 2323 Name name,
mcimadamore@302 2324 List<Type> argtypes,
mcimadamore@302 2325 List<Type> typeargtypes) {
mcimadamore@689 2326 if (candidates.nonEmpty()) {
mcimadamore@689 2327 JCDiagnostic err = diags.create(dkind,
mcimadamore@689 2328 log.currentSource(),
mcimadamore@689 2329 pos,
mcimadamore@689 2330 "cant.apply.symbols",
mcimadamore@689 2331 name == names.init ? KindName.CONSTRUCTOR : absentKind(kind),
mcimadamore@689 2332 getName(),
mcimadamore@689 2333 argtypes);
mcimadamore@689 2334 return new JCDiagnostic.MultilineDiagnostic(err, candidateDetails(site));
mcimadamore@689 2335 } else {
mcimadamore@689 2336 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind, pos,
mcimadamore@829 2337 location, site, name, argtypes, typeargtypes);
mcimadamore@689 2338 }
mcimadamore@689 2339 }
mcimadamore@689 2340
mcimadamore@689 2341 //where
mcimadamore@689 2342 List<JCDiagnostic> candidateDetails(Type site) {
mcimadamore@689 2343 List<JCDiagnostic> details = List.nil();
mcimadamore@689 2344 for (Candidate c : candidates)
mcimadamore@689 2345 details = details.prepend(c.getDiagnostic(site));
mcimadamore@689 2346 return details.reverse();
mcimadamore@689 2347 }
mcimadamore@689 2348
mcimadamore@689 2349 Symbol addCandidate(MethodResolutionPhase currentStep, Symbol sym, JCDiagnostic details) {
mcimadamore@689 2350 Candidate c = new Candidate(currentStep, sym, details);
mcimadamore@689 2351 if (c.isValid() && !candidates.contains(c))
mcimadamore@689 2352 candidates = candidates.append(c);
mcimadamore@689 2353 return this;
mcimadamore@689 2354 }
mcimadamore@689 2355
mcimadamore@689 2356 void clear() {
mcimadamore@689 2357 candidates = List.nil();
mcimadamore@689 2358 }
mcimadamore@689 2359
mcimadamore@689 2360 private Name getName() {
mcimadamore@689 2361 Symbol sym = candidates.head.sym;
mcimadamore@689 2362 return sym.name == names.init ?
mcimadamore@689 2363 sym.owner.name :
mcimadamore@689 2364 sym.name;
mcimadamore@689 2365 }
mcimadamore@689 2366
mcimadamore@689 2367 private class Candidate {
mcimadamore@689 2368
mcimadamore@689 2369 final MethodResolutionPhase step;
mcimadamore@689 2370 final Symbol sym;
mcimadamore@689 2371 final JCDiagnostic details;
mcimadamore@689 2372
mcimadamore@689 2373 private Candidate(MethodResolutionPhase step, Symbol sym, JCDiagnostic details) {
mcimadamore@689 2374 this.step = step;
mcimadamore@689 2375 this.sym = sym;
mcimadamore@689 2376 this.details = details;
mcimadamore@689 2377 }
mcimadamore@689 2378
mcimadamore@689 2379 JCDiagnostic getDiagnostic(Type site) {
mcimadamore@689 2380 return diags.fragment("inapplicable.method",
mcimadamore@689 2381 Kinds.kindName(sym),
mcimadamore@689 2382 sym.location(site, types),
mcimadamore@689 2383 sym.asMemberOf(site, types),
mcimadamore@689 2384 details);
mcimadamore@689 2385 }
mcimadamore@689 2386
mcimadamore@689 2387 @Override
mcimadamore@689 2388 public boolean equals(Object o) {
mcimadamore@689 2389 if (o instanceof Candidate) {
mcimadamore@689 2390 Symbol s1 = this.sym;
mcimadamore@689 2391 Symbol s2 = ((Candidate)o).sym;
mcimadamore@689 2392 if ((s1 != s2 &&
mcimadamore@689 2393 (s1.overrides(s2, s1.owner.type.tsym, types, false) ||
mcimadamore@689 2394 (s2.overrides(s1, s2.owner.type.tsym, types, false)))) ||
mcimadamore@689 2395 ((s1.isConstructor() || s2.isConstructor()) && s1.owner != s2.owner))
mcimadamore@689 2396 return true;
mcimadamore@689 2397 }
mcimadamore@689 2398 return false;
mcimadamore@689 2399 }
mcimadamore@689 2400
mcimadamore@689 2401 boolean isValid() {
mcimadamore@689 2402 return (((sym.flags() & VARARGS) != 0 && step == VARARITY) ||
mcimadamore@689 2403 (sym.flags() & VARARGS) == 0 && step == (boxingEnabled ? BOX : BASIC));
mcimadamore@689 2404 }
mcimadamore@302 2405 }
mcimadamore@302 2406 }
mcimadamore@302 2407
mcimadamore@302 2408 /**
mcimadamore@302 2409 * An InvalidSymbolError error class indicating that a symbol is not
mcimadamore@302 2410 * accessible from a given site
mcimadamore@302 2411 */
mcimadamore@302 2412 class AccessError extends InvalidSymbolError {
mcimadamore@302 2413
mcimadamore@302 2414 private Env<AttrContext> env;
mcimadamore@302 2415 private Type site;
duke@1 2416
duke@1 2417 AccessError(Symbol sym) {
duke@1 2418 this(null, null, sym);
duke@1 2419 }
duke@1 2420
duke@1 2421 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
duke@1 2422 super(HIDDEN, sym, "access error");
duke@1 2423 this.env = env;
duke@1 2424 this.site = site;
duke@1 2425 if (debugResolve)
duke@1 2426 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
duke@1 2427 }
duke@1 2428
mcimadamore@302 2429 @Override
mcimadamore@302 2430 public boolean exists() {
mcimadamore@302 2431 return false;
mcimadamore@302 2432 }
duke@1 2433
mcimadamore@302 2434 @Override
mcimadamore@302 2435 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2436 DiagnosticPosition pos,
mcimadamore@829 2437 Symbol location,
mcimadamore@302 2438 Type site,
mcimadamore@302 2439 Name name,
mcimadamore@302 2440 List<Type> argtypes,
mcimadamore@302 2441 List<Type> typeargtypes) {
mcimadamore@302 2442 if (sym.owner.type.tag == ERROR)
mcimadamore@302 2443 return null;
mcimadamore@302 2444
mcimadamore@302 2445 if (sym.name == names.init && sym.owner != site.tsym) {
mcimadamore@302 2446 return new SymbolNotFoundError(ABSENT_MTH).getDiagnostic(dkind,
mcimadamore@829 2447 pos, location, site, name, argtypes, typeargtypes);
mcimadamore@302 2448 }
mcimadamore@302 2449 else if ((sym.flags() & PUBLIC) != 0
mcimadamore@302 2450 || (env != null && this.site != null
mcimadamore@302 2451 && !isAccessible(env, this.site))) {
jjg@612 2452 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2453 pos, "not.def.access.class.intf.cant.access",
mcimadamore@302 2454 sym, sym.location());
mcimadamore@302 2455 }
mcimadamore@302 2456 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0) {
jjg@612 2457 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2458 pos, "report.access", sym,
mcimadamore@302 2459 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
mcimadamore@302 2460 sym.location());
mcimadamore@302 2461 }
mcimadamore@302 2462 else {
jjg@612 2463 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2464 pos, "not.def.public.cant.access", sym, sym.location());
duke@1 2465 }
duke@1 2466 }
duke@1 2467 }
duke@1 2468
mcimadamore@302 2469 /**
mcimadamore@302 2470 * InvalidSymbolError error class indicating that an instance member
mcimadamore@302 2471 * has erroneously been accessed from a static context.
duke@1 2472 */
mcimadamore@302 2473 class StaticError extends InvalidSymbolError {
mcimadamore@302 2474
duke@1 2475 StaticError(Symbol sym) {
duke@1 2476 super(STATICERR, sym, "static error");
duke@1 2477 }
duke@1 2478
mcimadamore@302 2479 @Override
mcimadamore@302 2480 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2481 DiagnosticPosition pos,
mcimadamore@829 2482 Symbol location,
mcimadamore@302 2483 Type site,
mcimadamore@302 2484 Name name,
mcimadamore@302 2485 List<Type> argtypes,
mcimadamore@302 2486 List<Type> typeargtypes) {
mcimadamore@80 2487 Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
mcimadamore@80 2488 ? types.erasure(sym.type).tsym
mcimadamore@80 2489 : sym);
jjg@612 2490 return diags.create(dkind, log.currentSource(), pos,
mcimadamore@302 2491 "non-static.cant.be.ref", kindName(sym), errSym);
duke@1 2492 }
duke@1 2493 }
duke@1 2494
mcimadamore@302 2495 /**
mcimadamore@302 2496 * InvalidSymbolError error class indicating that a pair of symbols
mcimadamore@302 2497 * (either methods, constructors or operands) are ambiguous
mcimadamore@302 2498 * given an actual arguments/type argument list.
duke@1 2499 */
mcimadamore@302 2500 class AmbiguityError extends InvalidSymbolError {
mcimadamore@302 2501
mcimadamore@302 2502 /** The other maximally specific symbol */
duke@1 2503 Symbol sym2;
duke@1 2504
duke@1 2505 AmbiguityError(Symbol sym1, Symbol sym2) {
duke@1 2506 super(AMBIGUOUS, sym1, "ambiguity error");
duke@1 2507 this.sym2 = sym2;
duke@1 2508 }
duke@1 2509
mcimadamore@302 2510 @Override
mcimadamore@302 2511 JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
mcimadamore@302 2512 DiagnosticPosition pos,
mcimadamore@829 2513 Symbol location,
mcimadamore@302 2514 Type site,
mcimadamore@302 2515 Name name,
mcimadamore@302 2516 List<Type> argtypes,
mcimadamore@302 2517 List<Type> typeargtypes) {
duke@1 2518 AmbiguityError pair = this;
duke@1 2519 while (true) {
mcimadamore@302 2520 if (pair.sym.kind == AMBIGUOUS)
mcimadamore@302 2521 pair = (AmbiguityError)pair.sym;
duke@1 2522 else if (pair.sym2.kind == AMBIGUOUS)
duke@1 2523 pair = (AmbiguityError)pair.sym2;
duke@1 2524 else break;
duke@1 2525 }
mcimadamore@302 2526 Name sname = pair.sym.name;
mcimadamore@302 2527 if (sname == names.init) sname = pair.sym.owner.name;
jjg@612 2528 return diags.create(dkind, log.currentSource(),
mcimadamore@302 2529 pos, "ref.ambiguous", sname,
mcimadamore@302 2530 kindName(pair.sym),
mcimadamore@302 2531 pair.sym,
mcimadamore@302 2532 pair.sym.location(site, types),
duke@1 2533 kindName(pair.sym2),
duke@1 2534 pair.sym2,
duke@1 2535 pair.sym2.location(site, types));
duke@1 2536 }
duke@1 2537 }
mcimadamore@160 2538
mcimadamore@160 2539 enum MethodResolutionPhase {
mcimadamore@160 2540 BASIC(false, false),
mcimadamore@160 2541 BOX(true, false),
mcimadamore@160 2542 VARARITY(true, true);
mcimadamore@160 2543
mcimadamore@160 2544 boolean isBoxingRequired;
mcimadamore@160 2545 boolean isVarargsRequired;
mcimadamore@160 2546
mcimadamore@160 2547 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
mcimadamore@160 2548 this.isBoxingRequired = isBoxingRequired;
mcimadamore@160 2549 this.isVarargsRequired = isVarargsRequired;
mcimadamore@160 2550 }
mcimadamore@160 2551
mcimadamore@160 2552 public boolean isBoxingRequired() {
mcimadamore@160 2553 return isBoxingRequired;
mcimadamore@160 2554 }
mcimadamore@160 2555
mcimadamore@160 2556 public boolean isVarargsRequired() {
mcimadamore@160 2557 return isVarargsRequired;
mcimadamore@160 2558 }
mcimadamore@160 2559
mcimadamore@160 2560 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
mcimadamore@160 2561 return (varargsEnabled || !isVarargsRequired) &&
mcimadamore@160 2562 (boxingEnabled || !isBoxingRequired);
mcimadamore@160 2563 }
mcimadamore@160 2564 }
mcimadamore@160 2565
mcimadamore@160 2566 private Map<MethodResolutionPhase, Symbol> methodResolutionCache =
mcimadamore@160 2567 new HashMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.values().length);
mcimadamore@160 2568
mcimadamore@1114 2569 private Map<Symbol, JCDiagnostic> verboseResolutionCandidateDiags =
mcimadamore@1114 2570 new LinkedHashMap<Symbol, JCDiagnostic>();
mcimadamore@1114 2571
mcimadamore@160 2572 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
mcimadamore@160 2573
mcimadamore@689 2574 private MethodResolutionPhase currentStep = null;
mcimadamore@689 2575
mcimadamore@1114 2576 private boolean internalResolution = false;
mcimadamore@1114 2577
mcimadamore@160 2578 private MethodResolutionPhase firstErroneousResolutionPhase() {
mcimadamore@160 2579 MethodResolutionPhase bestSoFar = BASIC;
mcimadamore@160 2580 Symbol sym = methodNotFound;
mcimadamore@160 2581 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 2582 while (steps.nonEmpty() &&
mcimadamore@160 2583 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 2584 sym.kind >= WRONG_MTHS) {
mcimadamore@160 2585 sym = methodResolutionCache.get(steps.head);
mcimadamore@160 2586 bestSoFar = steps.head;
mcimadamore@160 2587 steps = steps.tail;
mcimadamore@160 2588 }
mcimadamore@160 2589 return bestSoFar;
mcimadamore@160 2590 }
duke@1 2591 }

mercurial