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

Mon, 04 May 2009 21:04:04 -0700

author
jrose
date
Mon, 04 May 2009 21:04:04 -0700
changeset 267
e2722bd43f3a
parent 254
1ee128971f5d
child 299
22872b24d38c
permissions
-rw-r--r--

6829189: Java programming with JSR 292 needs language support
Summary: Language changes documented in http://wikis.sun.com/display/mlvm/ProjectCoinProposal
Reviewed-by: jjg, darcy, mcimadamore

duke@1 1 /*
xdono@54 2 * Copyright 1999-2008 Sun Microsystems, Inc. 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
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun 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 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.comp;
duke@1 27
duke@1 28 import com.sun.tools.javac.util.*;
duke@1 29 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 30 import com.sun.tools.javac.code.*;
duke@1 31 import com.sun.tools.javac.jvm.*;
duke@1 32 import com.sun.tools.javac.tree.*;
mcimadamore@161 33 import com.sun.tools.javac.api.Formattable.LocalizedString;
mcimadamore@160 34 import static com.sun.tools.javac.comp.Resolve.MethodResolutionPhase.*;
duke@1 35
duke@1 36 import com.sun.tools.javac.code.Type.*;
duke@1 37 import com.sun.tools.javac.code.Symbol.*;
duke@1 38 import com.sun.tools.javac.tree.JCTree.*;
duke@1 39
duke@1 40 import static com.sun.tools.javac.code.Flags.*;
duke@1 41 import static com.sun.tools.javac.code.Kinds.*;
duke@1 42 import static com.sun.tools.javac.code.TypeTags.*;
duke@1 43 import javax.lang.model.element.ElementVisitor;
duke@1 44
mcimadamore@160 45 import java.util.Map;
mcimadamore@160 46 import java.util.HashMap;
mcimadamore@160 47
duke@1 48 /** Helper class for name resolution, used mostly by the attribution phase.
duke@1 49 *
duke@1 50 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 51 * you write code that depends on this, you do so at your own risk.
duke@1 52 * This code and its internal interfaces are subject to change or
duke@1 53 * deletion without notice.</b>
duke@1 54 */
duke@1 55 public class Resolve {
duke@1 56 protected static final Context.Key<Resolve> resolveKey =
duke@1 57 new Context.Key<Resolve>();
duke@1 58
jjg@113 59 Names names;
duke@1 60 Log log;
duke@1 61 Symtab syms;
duke@1 62 Check chk;
duke@1 63 Infer infer;
duke@1 64 ClassReader reader;
duke@1 65 TreeInfo treeinfo;
duke@1 66 Types types;
mcimadamore@89 67 JCDiagnostic.Factory diags;
duke@1 68 public final boolean boxingEnabled; // = source.allowBoxing();
duke@1 69 public final boolean varargsEnabled; // = source.allowVarargs();
jrose@267 70 public final boolean allowInvokedynamic; // = options.get("invokedynamic");
duke@1 71 private final boolean debugResolve;
duke@1 72
duke@1 73 public static Resolve instance(Context context) {
duke@1 74 Resolve instance = context.get(resolveKey);
duke@1 75 if (instance == null)
duke@1 76 instance = new Resolve(context);
duke@1 77 return instance;
duke@1 78 }
duke@1 79
duke@1 80 protected Resolve(Context context) {
duke@1 81 context.put(resolveKey, this);
duke@1 82 syms = Symtab.instance(context);
duke@1 83
duke@1 84 varNotFound = new
duke@1 85 ResolveError(ABSENT_VAR, syms.errSymbol, "variable not found");
duke@1 86 wrongMethod = new
duke@1 87 ResolveError(WRONG_MTH, syms.errSymbol, "method not found");
duke@1 88 wrongMethods = new
duke@1 89 ResolveError(WRONG_MTHS, syms.errSymbol, "wrong methods");
duke@1 90 methodNotFound = new
duke@1 91 ResolveError(ABSENT_MTH, syms.errSymbol, "method not found");
duke@1 92 typeNotFound = new
duke@1 93 ResolveError(ABSENT_TYP, syms.errSymbol, "type not found");
duke@1 94
jjg@113 95 names = Names.instance(context);
duke@1 96 log = Log.instance(context);
duke@1 97 chk = Check.instance(context);
duke@1 98 infer = Infer.instance(context);
duke@1 99 reader = ClassReader.instance(context);
duke@1 100 treeinfo = TreeInfo.instance(context);
duke@1 101 types = Types.instance(context);
mcimadamore@89 102 diags = JCDiagnostic.Factory.instance(context);
duke@1 103 Source source = Source.instance(context);
duke@1 104 boxingEnabled = source.allowBoxing();
duke@1 105 varargsEnabled = source.allowVarargs();
duke@1 106 Options options = Options.instance(context);
duke@1 107 debugResolve = options.get("debugresolve") != null;
jrose@267 108 allowInvokedynamic = options.get("invokedynamic") != null;
duke@1 109 }
duke@1 110
duke@1 111 /** error symbols, which are returned when resolution fails
duke@1 112 */
duke@1 113 final ResolveError varNotFound;
duke@1 114 final ResolveError wrongMethod;
duke@1 115 final ResolveError wrongMethods;
duke@1 116 final ResolveError methodNotFound;
duke@1 117 final ResolveError typeNotFound;
duke@1 118
duke@1 119 /* ************************************************************************
duke@1 120 * Identifier resolution
duke@1 121 *************************************************************************/
duke@1 122
duke@1 123 /** An environment is "static" if its static level is greater than
duke@1 124 * the one of its outer environment
duke@1 125 */
duke@1 126 static boolean isStatic(Env<AttrContext> env) {
duke@1 127 return env.info.staticLevel > env.outer.info.staticLevel;
duke@1 128 }
duke@1 129
duke@1 130 /** An environment is an "initializer" if it is a constructor or
duke@1 131 * an instance initializer.
duke@1 132 */
duke@1 133 static boolean isInitializer(Env<AttrContext> env) {
duke@1 134 Symbol owner = env.info.scope.owner;
duke@1 135 return owner.isConstructor() ||
duke@1 136 owner.owner.kind == TYP &&
duke@1 137 (owner.kind == VAR ||
duke@1 138 owner.kind == MTH && (owner.flags() & BLOCK) != 0) &&
duke@1 139 (owner.flags() & STATIC) == 0;
duke@1 140 }
duke@1 141
duke@1 142 /** Is class accessible in given evironment?
duke@1 143 * @param env The current environment.
duke@1 144 * @param c The class whose accessibility is checked.
duke@1 145 */
duke@1 146 public boolean isAccessible(Env<AttrContext> env, TypeSymbol c) {
duke@1 147 switch ((short)(c.flags() & AccessFlags)) {
duke@1 148 case PRIVATE:
duke@1 149 return
duke@1 150 env.enclClass.sym.outermostClass() ==
duke@1 151 c.owner.outermostClass();
duke@1 152 case 0:
duke@1 153 return
duke@1 154 env.toplevel.packge == c.owner // fast special case
duke@1 155 ||
duke@1 156 env.toplevel.packge == c.packge()
duke@1 157 ||
duke@1 158 // Hack: this case is added since synthesized default constructors
duke@1 159 // of anonymous classes should be allowed to access
duke@1 160 // classes which would be inaccessible otherwise.
duke@1 161 env.enclMethod != null &&
duke@1 162 (env.enclMethod.mods.flags & ANONCONSTR) != 0;
duke@1 163 default: // error recovery
duke@1 164 case PUBLIC:
duke@1 165 return true;
duke@1 166 case PROTECTED:
duke@1 167 return
duke@1 168 env.toplevel.packge == c.owner // fast special case
duke@1 169 ||
duke@1 170 env.toplevel.packge == c.packge()
duke@1 171 ||
duke@1 172 isInnerSubClass(env.enclClass.sym, c.owner);
duke@1 173 }
duke@1 174 }
duke@1 175 //where
duke@1 176 /** Is given class a subclass of given base class, or an inner class
duke@1 177 * of a subclass?
duke@1 178 * Return null if no such class exists.
duke@1 179 * @param c The class which is the subclass or is contained in it.
duke@1 180 * @param base The base class
duke@1 181 */
duke@1 182 private boolean isInnerSubClass(ClassSymbol c, Symbol base) {
duke@1 183 while (c != null && !c.isSubClass(base, types)) {
duke@1 184 c = c.owner.enclClass();
duke@1 185 }
duke@1 186 return c != null;
duke@1 187 }
duke@1 188
duke@1 189 boolean isAccessible(Env<AttrContext> env, Type t) {
duke@1 190 return (t.tag == ARRAY)
duke@1 191 ? isAccessible(env, types.elemtype(t))
duke@1 192 : isAccessible(env, t.tsym);
duke@1 193 }
duke@1 194
duke@1 195 /** Is symbol accessible as a member of given type in given evironment?
duke@1 196 * @param env The current environment.
duke@1 197 * @param site The type of which the tested symbol is regarded
duke@1 198 * as a member.
duke@1 199 * @param sym The symbol.
duke@1 200 */
duke@1 201 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym) {
duke@1 202 if (sym.name == names.init && sym.owner != site.tsym) return false;
duke@1 203 ClassSymbol sub;
duke@1 204 switch ((short)(sym.flags() & AccessFlags)) {
duke@1 205 case PRIVATE:
duke@1 206 return
duke@1 207 (env.enclClass.sym == sym.owner // fast special case
duke@1 208 ||
duke@1 209 env.enclClass.sym.outermostClass() ==
duke@1 210 sym.owner.outermostClass())
duke@1 211 &&
duke@1 212 sym.isInheritedIn(site.tsym, types);
duke@1 213 case 0:
duke@1 214 return
duke@1 215 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 216 ||
duke@1 217 env.toplevel.packge == sym.packge())
duke@1 218 &&
duke@1 219 isAccessible(env, site)
duke@1 220 &&
mcimadamore@254 221 sym.isInheritedIn(site.tsym, types)
mcimadamore@254 222 &&
mcimadamore@254 223 notOverriddenIn(site, sym);
duke@1 224 case PROTECTED:
duke@1 225 return
duke@1 226 (env.toplevel.packge == sym.owner.owner // fast special case
duke@1 227 ||
duke@1 228 env.toplevel.packge == sym.packge()
duke@1 229 ||
duke@1 230 isProtectedAccessible(sym, env.enclClass.sym, site)
duke@1 231 ||
duke@1 232 // OK to select instance method or field from 'super' or type name
duke@1 233 // (but type names should be disallowed elsewhere!)
duke@1 234 env.info.selectSuper && (sym.flags() & STATIC) == 0 && sym.kind != TYP)
duke@1 235 &&
duke@1 236 isAccessible(env, site)
duke@1 237 &&
mcimadamore@254 238 notOverriddenIn(site, sym);
duke@1 239 default: // this case includes erroneous combinations as well
mcimadamore@254 240 return isAccessible(env, site) && notOverriddenIn(site, sym);
mcimadamore@254 241 }
mcimadamore@254 242 }
mcimadamore@254 243 //where
mcimadamore@254 244 /* `sym' is accessible only if not overridden by
mcimadamore@254 245 * another symbol which is a member of `site'
mcimadamore@254 246 * (because, if it is overridden, `sym' is not strictly
mcimadamore@254 247 * speaking a member of `site'.)
mcimadamore@254 248 */
mcimadamore@254 249 private boolean notOverriddenIn(Type site, Symbol sym) {
mcimadamore@254 250 if (sym.kind != MTH || sym.isConstructor() || sym.isStatic())
mcimadamore@254 251 return true;
mcimadamore@254 252 else {
mcimadamore@254 253 Symbol s2 = ((MethodSymbol)sym).implementation(site.tsym, types, true);
mcimadamore@254 254 return (s2 == null || s2 == sym);
duke@1 255 }
duke@1 256 }
duke@1 257 //where
duke@1 258 /** Is given protected symbol accessible if it is selected from given site
duke@1 259 * and the selection takes place in given class?
duke@1 260 * @param sym The symbol with protected access
duke@1 261 * @param c The class where the access takes place
duke@1 262 * @site The type of the qualifier
duke@1 263 */
duke@1 264 private
duke@1 265 boolean isProtectedAccessible(Symbol sym, ClassSymbol c, Type site) {
duke@1 266 while (c != null &&
duke@1 267 !(c.isSubClass(sym.owner, types) &&
duke@1 268 (c.flags() & INTERFACE) == 0 &&
duke@1 269 // In JLS 2e 6.6.2.1, the subclass restriction applies
duke@1 270 // only to instance fields and methods -- types are excluded
duke@1 271 // regardless of whether they are declared 'static' or not.
duke@1 272 ((sym.flags() & STATIC) != 0 || sym.kind == TYP || site.tsym.isSubClass(c, types))))
duke@1 273 c = c.owner.enclClass();
duke@1 274 return c != null;
duke@1 275 }
duke@1 276
duke@1 277 /** Try to instantiate the type of a method so that it fits
duke@1 278 * given type arguments and argument types. If succesful, return
duke@1 279 * the method's instantiated type, else return null.
duke@1 280 * The instantiation will take into account an additional leading
duke@1 281 * formal parameter if the method is an instance method seen as a member
duke@1 282 * of un underdetermined site In this case, we treat site as an additional
duke@1 283 * parameter and the parameters of the class containing the method as
duke@1 284 * additional type variables that get instantiated.
duke@1 285 *
duke@1 286 * @param env The current environment
duke@1 287 * @param site The type of which the method is a member.
duke@1 288 * @param m The method symbol.
duke@1 289 * @param argtypes The invocation's given value arguments.
duke@1 290 * @param typeargtypes The invocation's given type arguments.
duke@1 291 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 292 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 293 */
duke@1 294 Type rawInstantiate(Env<AttrContext> env,
duke@1 295 Type site,
duke@1 296 Symbol m,
duke@1 297 List<Type> argtypes,
duke@1 298 List<Type> typeargtypes,
duke@1 299 boolean allowBoxing,
duke@1 300 boolean useVarargs,
duke@1 301 Warner warn)
duke@1 302 throws Infer.NoInstanceException {
duke@1 303 if (useVarargs && (m.flags() & VARARGS) == 0) return null;
duke@1 304 Type mt = types.memberType(site, m);
duke@1 305
duke@1 306 // tvars is the list of formal type variables for which type arguments
duke@1 307 // need to inferred.
duke@1 308 List<Type> tvars = env.info.tvars;
duke@1 309 if (typeargtypes == null) typeargtypes = List.nil();
duke@1 310 if (mt.tag != FORALL && typeargtypes.nonEmpty()) {
duke@1 311 // This is not a polymorphic method, but typeargs are supplied
duke@1 312 // which is fine, see JLS3 15.12.2.1
duke@1 313 } else if (mt.tag == FORALL && typeargtypes.nonEmpty()) {
duke@1 314 ForAll pmt = (ForAll) mt;
duke@1 315 if (typeargtypes.length() != pmt.tvars.length())
duke@1 316 return null;
duke@1 317 // Check type arguments are within bounds
duke@1 318 List<Type> formals = pmt.tvars;
duke@1 319 List<Type> actuals = typeargtypes;
duke@1 320 while (formals.nonEmpty() && actuals.nonEmpty()) {
duke@1 321 List<Type> bounds = types.subst(types.getBounds((TypeVar)formals.head),
duke@1 322 pmt.tvars, typeargtypes);
duke@1 323 for (; bounds.nonEmpty(); bounds = bounds.tail)
duke@1 324 if (!types.isSubtypeUnchecked(actuals.head, bounds.head, warn))
duke@1 325 return null;
duke@1 326 formals = formals.tail;
duke@1 327 actuals = actuals.tail;
duke@1 328 }
duke@1 329 mt = types.subst(pmt.qtype, pmt.tvars, typeargtypes);
duke@1 330 } else if (mt.tag == FORALL) {
duke@1 331 ForAll pmt = (ForAll) mt;
duke@1 332 List<Type> tvars1 = types.newInstances(pmt.tvars);
duke@1 333 tvars = tvars.appendList(tvars1);
duke@1 334 mt = types.subst(pmt.qtype, pmt.tvars, tvars1);
duke@1 335 }
duke@1 336
duke@1 337 // find out whether we need to go the slow route via infer
duke@1 338 boolean instNeeded = tvars.tail != null/*inlined: tvars.nonEmpty()*/;
duke@1 339 for (List<Type> l = argtypes;
duke@1 340 l.tail != null/*inlined: l.nonEmpty()*/ && !instNeeded;
duke@1 341 l = l.tail) {
duke@1 342 if (l.head.tag == FORALL) instNeeded = true;
duke@1 343 }
duke@1 344
duke@1 345 if (instNeeded)
duke@1 346 return
duke@1 347 infer.instantiateMethod(tvars,
duke@1 348 (MethodType)mt,
duke@1 349 argtypes,
duke@1 350 allowBoxing,
duke@1 351 useVarargs,
duke@1 352 warn);
duke@1 353 return
duke@1 354 argumentsAcceptable(argtypes, mt.getParameterTypes(),
duke@1 355 allowBoxing, useVarargs, warn)
duke@1 356 ? mt
duke@1 357 : null;
duke@1 358 }
duke@1 359
duke@1 360 /** Same but returns null instead throwing a NoInstanceException
duke@1 361 */
duke@1 362 Type instantiate(Env<AttrContext> env,
duke@1 363 Type site,
duke@1 364 Symbol m,
duke@1 365 List<Type> argtypes,
duke@1 366 List<Type> typeargtypes,
duke@1 367 boolean allowBoxing,
duke@1 368 boolean useVarargs,
duke@1 369 Warner warn) {
duke@1 370 try {
duke@1 371 return rawInstantiate(env, site, m, argtypes, typeargtypes,
duke@1 372 allowBoxing, useVarargs, warn);
duke@1 373 } catch (Infer.NoInstanceException ex) {
duke@1 374 return null;
duke@1 375 }
duke@1 376 }
duke@1 377
duke@1 378 /** Check if a parameter list accepts a list of args.
duke@1 379 */
duke@1 380 boolean argumentsAcceptable(List<Type> argtypes,
duke@1 381 List<Type> formals,
duke@1 382 boolean allowBoxing,
duke@1 383 boolean useVarargs,
duke@1 384 Warner warn) {
duke@1 385 Type varargsFormal = useVarargs ? formals.last() : null;
duke@1 386 while (argtypes.nonEmpty() && formals.head != varargsFormal) {
duke@1 387 boolean works = allowBoxing
duke@1 388 ? types.isConvertible(argtypes.head, formals.head, warn)
duke@1 389 : types.isSubtypeUnchecked(argtypes.head, formals.head, warn);
duke@1 390 if (!works) return false;
duke@1 391 argtypes = argtypes.tail;
duke@1 392 formals = formals.tail;
duke@1 393 }
duke@1 394 if (formals.head != varargsFormal) return false; // not enough args
duke@1 395 if (!useVarargs)
duke@1 396 return argtypes.isEmpty();
duke@1 397 Type elt = types.elemtype(varargsFormal);
duke@1 398 while (argtypes.nonEmpty()) {
duke@1 399 if (!types.isConvertible(argtypes.head, elt, warn))
duke@1 400 return false;
duke@1 401 argtypes = argtypes.tail;
duke@1 402 }
duke@1 403 return true;
duke@1 404 }
duke@1 405
duke@1 406 /* ***************************************************************************
duke@1 407 * Symbol lookup
duke@1 408 * the following naming conventions for arguments are used
duke@1 409 *
duke@1 410 * env is the environment where the symbol was mentioned
duke@1 411 * site is the type of which the symbol is a member
duke@1 412 * name is the symbol's name
duke@1 413 * if no arguments are given
duke@1 414 * argtypes are the value arguments, if we search for a method
duke@1 415 *
duke@1 416 * If no symbol was found, a ResolveError detailing the problem is returned.
duke@1 417 ****************************************************************************/
duke@1 418
duke@1 419 /** Find field. Synthetic fields are always skipped.
duke@1 420 * @param env The current environment.
duke@1 421 * @param site The original type from where the selection takes place.
duke@1 422 * @param name The name of the field.
duke@1 423 * @param c The class to search for the field. This is always
duke@1 424 * a superclass or implemented interface of site's class.
duke@1 425 */
duke@1 426 Symbol findField(Env<AttrContext> env,
duke@1 427 Type site,
duke@1 428 Name name,
duke@1 429 TypeSymbol c) {
mcimadamore@19 430 while (c.type.tag == TYPEVAR)
mcimadamore@19 431 c = c.type.getUpperBound().tsym;
duke@1 432 Symbol bestSoFar = varNotFound;
duke@1 433 Symbol sym;
duke@1 434 Scope.Entry e = c.members().lookup(name);
duke@1 435 while (e.scope != null) {
duke@1 436 if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 437 return isAccessible(env, site, e.sym)
duke@1 438 ? e.sym : new AccessError(env, site, e.sym);
duke@1 439 }
duke@1 440 e = e.next();
duke@1 441 }
duke@1 442 Type st = types.supertype(c.type);
mcimadamore@19 443 if (st != null && (st.tag == CLASS || st.tag == TYPEVAR)) {
duke@1 444 sym = findField(env, site, name, st.tsym);
duke@1 445 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 446 }
duke@1 447 for (List<Type> l = types.interfaces(c.type);
duke@1 448 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 449 l = l.tail) {
duke@1 450 sym = findField(env, site, name, l.head.tsym);
duke@1 451 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 452 sym.owner != bestSoFar.owner)
duke@1 453 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 454 else if (sym.kind < bestSoFar.kind)
duke@1 455 bestSoFar = sym;
duke@1 456 }
duke@1 457 return bestSoFar;
duke@1 458 }
duke@1 459
duke@1 460 /** Resolve a field identifier, throw a fatal error if not found.
duke@1 461 * @param pos The position to use for error reporting.
duke@1 462 * @param env The environment current at the method invocation.
duke@1 463 * @param site The type of the qualifying expression, in which
duke@1 464 * identifier is searched.
duke@1 465 * @param name The identifier's name.
duke@1 466 */
duke@1 467 public VarSymbol resolveInternalField(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 468 Type site, Name name) {
duke@1 469 Symbol sym = findField(env, site, name, site.tsym);
duke@1 470 if (sym.kind == VAR) return (VarSymbol)sym;
duke@1 471 else throw new FatalError(
mcimadamore@89 472 diags.fragment("fatal.err.cant.locate.field",
duke@1 473 name));
duke@1 474 }
duke@1 475
duke@1 476 /** Find unqualified variable or field with given name.
duke@1 477 * Synthetic fields always skipped.
duke@1 478 * @param env The current environment.
duke@1 479 * @param name The name of the variable or field.
duke@1 480 */
duke@1 481 Symbol findVar(Env<AttrContext> env, Name name) {
duke@1 482 Symbol bestSoFar = varNotFound;
duke@1 483 Symbol sym;
duke@1 484 Env<AttrContext> env1 = env;
duke@1 485 boolean staticOnly = false;
duke@1 486 while (env1.outer != null) {
duke@1 487 if (isStatic(env1)) staticOnly = true;
duke@1 488 Scope.Entry e = env1.info.scope.lookup(name);
duke@1 489 while (e.scope != null &&
duke@1 490 (e.sym.kind != VAR ||
duke@1 491 (e.sym.flags_field & SYNTHETIC) != 0))
duke@1 492 e = e.next();
duke@1 493 sym = (e.scope != null)
duke@1 494 ? e.sym
duke@1 495 : findField(
duke@1 496 env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
duke@1 497 if (sym.exists()) {
duke@1 498 if (staticOnly &&
duke@1 499 sym.kind == VAR &&
duke@1 500 sym.owner.kind == TYP &&
duke@1 501 (sym.flags() & STATIC) == 0)
duke@1 502 return new StaticError(sym);
duke@1 503 else
duke@1 504 return sym;
duke@1 505 } else if (sym.kind < bestSoFar.kind) {
duke@1 506 bestSoFar = sym;
duke@1 507 }
duke@1 508
duke@1 509 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 510 env1 = env1.outer;
duke@1 511 }
duke@1 512
duke@1 513 sym = findField(env, syms.predefClass.type, name, syms.predefClass);
duke@1 514 if (sym.exists())
duke@1 515 return sym;
duke@1 516 if (bestSoFar.exists())
duke@1 517 return bestSoFar;
duke@1 518
duke@1 519 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 520 for (; e.scope != null; e = e.next()) {
duke@1 521 sym = e.sym;
duke@1 522 Type origin = e.getOrigin().owner.type;
duke@1 523 if (sym.kind == VAR) {
duke@1 524 if (e.sym.owner.type != origin)
duke@1 525 sym = sym.clone(e.getOrigin().owner);
duke@1 526 return isAccessible(env, origin, sym)
duke@1 527 ? sym : new AccessError(env, origin, sym);
duke@1 528 }
duke@1 529 }
duke@1 530
duke@1 531 Symbol origin = null;
duke@1 532 e = env.toplevel.starImportScope.lookup(name);
duke@1 533 for (; e.scope != null; e = e.next()) {
duke@1 534 sym = e.sym;
duke@1 535 if (sym.kind != VAR)
duke@1 536 continue;
duke@1 537 // invariant: sym.kind == VAR
duke@1 538 if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
duke@1 539 return new AmbiguityError(bestSoFar, sym);
duke@1 540 else if (bestSoFar.kind >= VAR) {
duke@1 541 origin = e.getOrigin().owner;
duke@1 542 bestSoFar = isAccessible(env, origin.type, sym)
duke@1 543 ? sym : new AccessError(env, origin.type, sym);
duke@1 544 }
duke@1 545 }
duke@1 546 if (bestSoFar.kind == VAR && bestSoFar.owner.type != origin.type)
duke@1 547 return bestSoFar.clone(origin);
duke@1 548 else
duke@1 549 return bestSoFar;
duke@1 550 }
duke@1 551
duke@1 552 Warner noteWarner = new Warner();
duke@1 553
duke@1 554 /** Select the best method for a call site among two choices.
duke@1 555 * @param env The current environment.
duke@1 556 * @param site The original type from where the
duke@1 557 * selection takes place.
duke@1 558 * @param argtypes The invocation's value arguments,
duke@1 559 * @param typeargtypes The invocation's type arguments,
duke@1 560 * @param sym Proposed new best match.
duke@1 561 * @param bestSoFar Previously found best match.
duke@1 562 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 563 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 564 */
duke@1 565 Symbol selectBest(Env<AttrContext> env,
duke@1 566 Type site,
duke@1 567 List<Type> argtypes,
duke@1 568 List<Type> typeargtypes,
duke@1 569 Symbol sym,
duke@1 570 Symbol bestSoFar,
duke@1 571 boolean allowBoxing,
duke@1 572 boolean useVarargs,
duke@1 573 boolean operator) {
duke@1 574 if (sym.kind == ERR) return bestSoFar;
mcimadamore@171 575 if (!sym.isInheritedIn(site.tsym, types)) return bestSoFar;
duke@1 576 assert sym.kind < AMBIGUOUS;
duke@1 577 try {
duke@1 578 if (rawInstantiate(env, site, sym, argtypes, typeargtypes,
duke@1 579 allowBoxing, useVarargs, Warner.noWarnings) == null) {
duke@1 580 // inapplicable
duke@1 581 switch (bestSoFar.kind) {
duke@1 582 case ABSENT_MTH: return wrongMethod.setWrongSym(sym);
duke@1 583 case WRONG_MTH: return wrongMethods;
duke@1 584 default: return bestSoFar;
duke@1 585 }
duke@1 586 }
duke@1 587 } catch (Infer.NoInstanceException ex) {
duke@1 588 switch (bestSoFar.kind) {
duke@1 589 case ABSENT_MTH:
duke@1 590 return wrongMethod.setWrongSym(sym, ex.getDiagnostic());
duke@1 591 case WRONG_MTH:
duke@1 592 return wrongMethods;
duke@1 593 default:
duke@1 594 return bestSoFar;
duke@1 595 }
duke@1 596 }
duke@1 597 if (!isAccessible(env, site, sym)) {
duke@1 598 return (bestSoFar.kind == ABSENT_MTH)
duke@1 599 ? new AccessError(env, site, sym)
duke@1 600 : bestSoFar;
duke@1 601 }
duke@1 602 return (bestSoFar.kind > AMBIGUOUS)
duke@1 603 ? sym
duke@1 604 : mostSpecific(sym, bestSoFar, env, site,
duke@1 605 allowBoxing && operator, useVarargs);
duke@1 606 }
duke@1 607
duke@1 608 /* Return the most specific of the two methods for a call,
duke@1 609 * given that both are accessible and applicable.
duke@1 610 * @param m1 A new candidate for most specific.
duke@1 611 * @param m2 The previous most specific candidate.
duke@1 612 * @param env The current environment.
duke@1 613 * @param site The original type from where the selection
duke@1 614 * takes place.
duke@1 615 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 616 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 617 */
duke@1 618 Symbol mostSpecific(Symbol m1,
duke@1 619 Symbol m2,
duke@1 620 Env<AttrContext> env,
mcimadamore@254 621 final Type site,
duke@1 622 boolean allowBoxing,
duke@1 623 boolean useVarargs) {
duke@1 624 switch (m2.kind) {
duke@1 625 case MTH:
duke@1 626 if (m1 == m2) return m1;
duke@1 627 Type mt1 = types.memberType(site, m1);
duke@1 628 noteWarner.unchecked = false;
duke@1 629 boolean m1SignatureMoreSpecific =
duke@1 630 (instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
duke@1 631 allowBoxing, false, noteWarner) != null ||
duke@1 632 useVarargs && instantiate(env, site, m2, types.lowerBoundArgtypes(mt1), null,
duke@1 633 allowBoxing, true, noteWarner) != null) &&
duke@1 634 !noteWarner.unchecked;
duke@1 635 Type mt2 = types.memberType(site, m2);
duke@1 636 noteWarner.unchecked = false;
duke@1 637 boolean m2SignatureMoreSpecific =
duke@1 638 (instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
duke@1 639 allowBoxing, false, noteWarner) != null ||
duke@1 640 useVarargs && instantiate(env, site, m1, types.lowerBoundArgtypes(mt2), null,
duke@1 641 allowBoxing, true, noteWarner) != null) &&
duke@1 642 !noteWarner.unchecked;
duke@1 643 if (m1SignatureMoreSpecific && m2SignatureMoreSpecific) {
duke@1 644 if (!types.overrideEquivalent(mt1, mt2))
duke@1 645 return new AmbiguityError(m1, m2);
duke@1 646 // same signature; select (a) the non-bridge method, or
duke@1 647 // (b) the one that overrides the other, or (c) the concrete
duke@1 648 // one, or (d) merge both abstract signatures
duke@1 649 if ((m1.flags() & BRIDGE) != (m2.flags() & BRIDGE)) {
duke@1 650 return ((m1.flags() & BRIDGE) != 0) ? m2 : m1;
duke@1 651 }
duke@1 652 // if one overrides or hides the other, use it
duke@1 653 TypeSymbol m1Owner = (TypeSymbol)m1.owner;
duke@1 654 TypeSymbol m2Owner = (TypeSymbol)m2.owner;
duke@1 655 if (types.asSuper(m1Owner.type, m2Owner) != null &&
duke@1 656 ((m1.owner.flags_field & INTERFACE) == 0 ||
duke@1 657 (m2.owner.flags_field & INTERFACE) != 0) &&
duke@1 658 m1.overrides(m2, m1Owner, types, false))
duke@1 659 return m1;
duke@1 660 if (types.asSuper(m2Owner.type, m1Owner) != null &&
duke@1 661 ((m2.owner.flags_field & INTERFACE) == 0 ||
duke@1 662 (m1.owner.flags_field & INTERFACE) != 0) &&
duke@1 663 m2.overrides(m1, m2Owner, types, false))
duke@1 664 return m2;
duke@1 665 boolean m1Abstract = (m1.flags() & ABSTRACT) != 0;
duke@1 666 boolean m2Abstract = (m2.flags() & ABSTRACT) != 0;
duke@1 667 if (m1Abstract && !m2Abstract) return m2;
duke@1 668 if (m2Abstract && !m1Abstract) return m1;
duke@1 669 // both abstract or both concrete
duke@1 670 if (!m1Abstract && !m2Abstract)
duke@1 671 return new AmbiguityError(m1, m2);
mcimadamore@156 672 // check that both signatures have the same erasure
mcimadamore@156 673 if (!types.isSameTypes(m1.erasure(types).getParameterTypes(),
mcimadamore@156 674 m2.erasure(types).getParameterTypes()))
duke@1 675 return new AmbiguityError(m1, m2);
duke@1 676 // both abstract, neither overridden; merge throws clause and result type
mcimadamore@254 677 Symbol mostSpecific;
jjg@110 678 Type result2 = mt2.getReturnType();
duke@1 679 if (mt2.tag == FORALL)
duke@1 680 result2 = types.subst(result2, ((ForAll)mt2).tvars, ((ForAll)mt1).tvars);
duke@1 681 if (types.isSubtype(mt1.getReturnType(), result2)) {
mcimadamore@254 682 mostSpecific = m1;
duke@1 683 } else if (types.isSubtype(result2, mt1.getReturnType())) {
mcimadamore@254 684 mostSpecific = m2;
duke@1 685 } else {
duke@1 686 // Theoretically, this can't happen, but it is possible
duke@1 687 // due to error recovery or mixing incompatible class files
duke@1 688 return new AmbiguityError(m1, m2);
duke@1 689 }
mcimadamore@254 690 MethodSymbol result = new MethodSymbol(
mcimadamore@254 691 mostSpecific.flags(),
mcimadamore@254 692 mostSpecific.name,
mcimadamore@254 693 null,
mcimadamore@254 694 mostSpecific.owner) {
mcimadamore@254 695 @Override
mcimadamore@254 696 public MethodSymbol implementation(TypeSymbol origin, Types types, boolean checkResult) {
mcimadamore@254 697 if (origin == site.tsym)
mcimadamore@254 698 return this;
mcimadamore@254 699 else
mcimadamore@254 700 return super.implementation(origin, types, checkResult);
mcimadamore@254 701 }
mcimadamore@254 702 };
mcimadamore@254 703 result.type = (Type)mostSpecific.type.clone();
duke@1 704 result.type.setThrown(chk.intersect(mt1.getThrownTypes(),
duke@1 705 mt2.getThrownTypes()));
duke@1 706 return result;
duke@1 707 }
duke@1 708 if (m1SignatureMoreSpecific) return m1;
duke@1 709 if (m2SignatureMoreSpecific) return m2;
duke@1 710 return new AmbiguityError(m1, m2);
duke@1 711 case AMBIGUOUS:
duke@1 712 AmbiguityError e = (AmbiguityError)m2;
duke@1 713 Symbol err1 = mostSpecific(m1, e.sym1, env, site, allowBoxing, useVarargs);
duke@1 714 Symbol err2 = mostSpecific(m1, e.sym2, env, site, allowBoxing, useVarargs);
duke@1 715 if (err1 == err2) return err1;
duke@1 716 if (err1 == e.sym1 && err2 == e.sym2) return m2;
duke@1 717 if (err1 instanceof AmbiguityError &&
duke@1 718 err2 instanceof AmbiguityError &&
duke@1 719 ((AmbiguityError)err1).sym1 == ((AmbiguityError)err2).sym1)
duke@1 720 return new AmbiguityError(m1, m2);
duke@1 721 else
duke@1 722 return new AmbiguityError(err1, err2);
duke@1 723 default:
duke@1 724 throw new AssertionError();
duke@1 725 }
duke@1 726 }
duke@1 727
duke@1 728 /** Find best qualified method matching given name, type and value
duke@1 729 * arguments.
duke@1 730 * @param env The current environment.
duke@1 731 * @param site The original type from where the selection
duke@1 732 * takes place.
duke@1 733 * @param name The method's name.
duke@1 734 * @param argtypes The method's value arguments.
duke@1 735 * @param typeargtypes The method's type arguments
duke@1 736 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 737 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 738 */
duke@1 739 Symbol findMethod(Env<AttrContext> env,
duke@1 740 Type site,
duke@1 741 Name name,
duke@1 742 List<Type> argtypes,
duke@1 743 List<Type> typeargtypes,
duke@1 744 boolean allowBoxing,
duke@1 745 boolean useVarargs,
duke@1 746 boolean operator) {
duke@1 747 return findMethod(env,
duke@1 748 site,
duke@1 749 name,
duke@1 750 argtypes,
duke@1 751 typeargtypes,
duke@1 752 site.tsym.type,
duke@1 753 true,
duke@1 754 methodNotFound,
duke@1 755 allowBoxing,
duke@1 756 useVarargs,
duke@1 757 operator);
duke@1 758 }
duke@1 759 // where
duke@1 760 private Symbol findMethod(Env<AttrContext> env,
duke@1 761 Type site,
duke@1 762 Name name,
duke@1 763 List<Type> argtypes,
duke@1 764 List<Type> typeargtypes,
duke@1 765 Type intype,
duke@1 766 boolean abstractok,
duke@1 767 Symbol bestSoFar,
duke@1 768 boolean allowBoxing,
duke@1 769 boolean useVarargs,
duke@1 770 boolean operator) {
mcimadamore@19 771 for (Type ct = intype; ct.tag == CLASS || ct.tag == TYPEVAR; ct = types.supertype(ct)) {
mcimadamore@19 772 while (ct.tag == TYPEVAR)
mcimadamore@19 773 ct = ct.getUpperBound();
duke@1 774 ClassSymbol c = (ClassSymbol)ct.tsym;
mcimadamore@135 775 if ((c.flags() & (ABSTRACT | INTERFACE | ENUM)) == 0)
duke@1 776 abstractok = false;
duke@1 777 for (Scope.Entry e = c.members().lookup(name);
duke@1 778 e.scope != null;
duke@1 779 e = e.next()) {
duke@1 780 //- System.out.println(" e " + e.sym);
duke@1 781 if (e.sym.kind == MTH &&
duke@1 782 (e.sym.flags_field & SYNTHETIC) == 0) {
duke@1 783 bestSoFar = selectBest(env, site, argtypes, typeargtypes,
duke@1 784 e.sym, bestSoFar,
duke@1 785 allowBoxing,
duke@1 786 useVarargs,
duke@1 787 operator);
duke@1 788 }
duke@1 789 }
duke@1 790 //- System.out.println(" - " + bestSoFar);
duke@1 791 if (abstractok) {
duke@1 792 Symbol concrete = methodNotFound;
duke@1 793 if ((bestSoFar.flags() & ABSTRACT) == 0)
duke@1 794 concrete = bestSoFar;
duke@1 795 for (List<Type> l = types.interfaces(c.type);
duke@1 796 l.nonEmpty();
duke@1 797 l = l.tail) {
duke@1 798 bestSoFar = findMethod(env, site, name, argtypes,
duke@1 799 typeargtypes,
duke@1 800 l.head, abstractok, bestSoFar,
duke@1 801 allowBoxing, useVarargs, operator);
duke@1 802 }
duke@1 803 if (concrete != bestSoFar &&
duke@1 804 concrete.kind < ERR && bestSoFar.kind < ERR &&
duke@1 805 types.isSubSignature(concrete.type, bestSoFar.type))
duke@1 806 bestSoFar = concrete;
duke@1 807 }
duke@1 808 }
duke@1 809 return bestSoFar;
duke@1 810 }
duke@1 811
duke@1 812 /** Find unqualified method matching given name, type and value arguments.
duke@1 813 * @param env The current environment.
duke@1 814 * @param name The method's name.
duke@1 815 * @param argtypes The method's value arguments.
duke@1 816 * @param typeargtypes The method's type arguments.
duke@1 817 * @param allowBoxing Allow boxing conversions of arguments.
duke@1 818 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 819 */
duke@1 820 Symbol findFun(Env<AttrContext> env, Name name,
duke@1 821 List<Type> argtypes, List<Type> typeargtypes,
duke@1 822 boolean allowBoxing, boolean useVarargs) {
duke@1 823 Symbol bestSoFar = methodNotFound;
duke@1 824 Symbol sym;
duke@1 825 Env<AttrContext> env1 = env;
duke@1 826 boolean staticOnly = false;
duke@1 827 while (env1.outer != null) {
duke@1 828 if (isStatic(env1)) staticOnly = true;
duke@1 829 sym = findMethod(
duke@1 830 env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
duke@1 831 allowBoxing, useVarargs, false);
duke@1 832 if (sym.exists()) {
duke@1 833 if (staticOnly &&
duke@1 834 sym.kind == MTH &&
duke@1 835 sym.owner.kind == TYP &&
duke@1 836 (sym.flags() & STATIC) == 0) return new StaticError(sym);
duke@1 837 else return sym;
duke@1 838 } else if (sym.kind < bestSoFar.kind) {
duke@1 839 bestSoFar = sym;
duke@1 840 }
duke@1 841 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 842 env1 = env1.outer;
duke@1 843 }
duke@1 844
duke@1 845 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 846 typeargtypes, allowBoxing, useVarargs, false);
duke@1 847 if (sym.exists())
duke@1 848 return sym;
duke@1 849
duke@1 850 Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
duke@1 851 for (; e.scope != null; e = e.next()) {
duke@1 852 sym = e.sym;
duke@1 853 Type origin = e.getOrigin().owner.type;
duke@1 854 if (sym.kind == MTH) {
duke@1 855 if (e.sym.owner.type != origin)
duke@1 856 sym = sym.clone(e.getOrigin().owner);
duke@1 857 if (!isAccessible(env, origin, sym))
duke@1 858 sym = new AccessError(env, origin, sym);
duke@1 859 bestSoFar = selectBest(env, origin,
duke@1 860 argtypes, typeargtypes,
duke@1 861 sym, bestSoFar,
duke@1 862 allowBoxing, useVarargs, false);
duke@1 863 }
duke@1 864 }
duke@1 865 if (bestSoFar.exists())
duke@1 866 return bestSoFar;
duke@1 867
duke@1 868 e = env.toplevel.starImportScope.lookup(name);
duke@1 869 for (; e.scope != null; e = e.next()) {
duke@1 870 sym = e.sym;
duke@1 871 Type origin = e.getOrigin().owner.type;
duke@1 872 if (sym.kind == MTH) {
duke@1 873 if (e.sym.owner.type != origin)
duke@1 874 sym = sym.clone(e.getOrigin().owner);
duke@1 875 if (!isAccessible(env, origin, sym))
duke@1 876 sym = new AccessError(env, origin, sym);
duke@1 877 bestSoFar = selectBest(env, origin,
duke@1 878 argtypes, typeargtypes,
duke@1 879 sym, bestSoFar,
duke@1 880 allowBoxing, useVarargs, false);
duke@1 881 }
duke@1 882 }
duke@1 883 return bestSoFar;
duke@1 884 }
duke@1 885
jrose@267 886 /** Find or create an implicit method of exactly the given type (after erasure).
jrose@267 887 * Searches in a side table, not the main scope of the site.
jrose@267 888 * This emulates the lookup process required by JSR 292 in JVM.
jrose@267 889 * @param env The current environment.
jrose@267 890 * @param site The original type from where the selection
jrose@267 891 * takes place.
jrose@267 892 * @param name The method's name.
jrose@267 893 * @param argtypes The method's value arguments.
jrose@267 894 * @param typeargtypes The method's type arguments
jrose@267 895 */
jrose@267 896 Symbol findImplicitMethod(Env<AttrContext> env,
jrose@267 897 Type site,
jrose@267 898 Name name,
jrose@267 899 List<Type> argtypes,
jrose@267 900 List<Type> typeargtypes) {
jrose@267 901 assert allowInvokedynamic;
jrose@267 902 assert site == syms.invokeDynamicType || (site == syms.methodHandleType && name == names.invoke);
jrose@267 903 ClassSymbol c = (ClassSymbol) site.tsym;
jrose@267 904 Scope implicit = c.members().next;
jrose@267 905 if (implicit == null) {
jrose@267 906 c.members().next = implicit = new Scope(c);
jrose@267 907 }
jrose@267 908 Type restype;
jrose@267 909 if (typeargtypes.isEmpty()) {
jrose@267 910 restype = syms.objectType;
jrose@267 911 } else {
jrose@267 912 restype = typeargtypes.head;
jrose@267 913 if (!typeargtypes.tail.isEmpty())
jrose@267 914 return methodNotFound;
jrose@267 915 }
jrose@267 916 List<Type> paramtypes = Type.map(argtypes, implicitArgType);
jrose@267 917 MethodType mtype = new MethodType(paramtypes,
jrose@267 918 restype,
jrose@267 919 List.<Type>nil(),
jrose@267 920 syms.methodClass);
jrose@267 921 int flags = PUBLIC | ABSTRACT;
jrose@267 922 if (site == syms.invokeDynamicType) flags |= STATIC;
jrose@267 923 Symbol m = null;
jrose@267 924 for (Scope.Entry e = implicit.lookup(name);
jrose@267 925 e.scope != null;
jrose@267 926 e = e.next()) {
jrose@267 927 Symbol sym = e.sym;
jrose@267 928 assert sym.kind == MTH;
jrose@267 929 if (types.isSameType(mtype, sym.type)
jrose@267 930 && (sym.flags() & STATIC) == (flags & STATIC)) {
jrose@267 931 m = sym;
jrose@267 932 break;
jrose@267 933 }
jrose@267 934 }
jrose@267 935 if (m == null) {
jrose@267 936 // create the desired method
jrose@267 937 m = new MethodSymbol(flags, name, mtype, c);
jrose@267 938 implicit.enter(m);
jrose@267 939 }
jrose@267 940 assert argumentsAcceptable(argtypes, types.memberType(site, m).getParameterTypes(),
jrose@267 941 false, false, Warner.noWarnings);
jrose@267 942 assert null != instantiate(env, site, m, argtypes, typeargtypes, false, false, Warner.noWarnings);
jrose@267 943 return m;
jrose@267 944 }
jrose@267 945 //where
jrose@267 946 Mapping implicitArgType = new Mapping ("implicitArgType") {
jrose@267 947 public Type apply(Type t) { return implicitArgType(t); }
jrose@267 948 };
jrose@267 949 Type implicitArgType(Type argType) {
jrose@267 950 argType = types.erasure(argType);
jrose@267 951 if (argType.tag == BOT)
jrose@267 952 // nulls type as the marker type Null (which has no instances)
jrose@267 953 // TO DO: figure out how to access java.lang.Null safely, else throw nice error
jrose@267 954 //argType = types.boxedClass(syms.botType).type;
jrose@267 955 argType = types.boxedClass(syms.voidType).type; // REMOVE
jrose@267 956 return argType;
jrose@267 957 }
jrose@267 958
duke@1 959 /** Load toplevel or member class with given fully qualified name and
duke@1 960 * verify that it is accessible.
duke@1 961 * @param env The current environment.
duke@1 962 * @param name The fully qualified name of the class to be loaded.
duke@1 963 */
duke@1 964 Symbol loadClass(Env<AttrContext> env, Name name) {
duke@1 965 try {
duke@1 966 ClassSymbol c = reader.loadClass(name);
duke@1 967 return isAccessible(env, c) ? c : new AccessError(c);
duke@1 968 } catch (ClassReader.BadClassFile err) {
duke@1 969 throw err;
duke@1 970 } catch (CompletionFailure ex) {
duke@1 971 return typeNotFound;
duke@1 972 }
duke@1 973 }
duke@1 974
duke@1 975 /** Find qualified member type.
duke@1 976 * @param env The current environment.
duke@1 977 * @param site The original type from where the selection takes
duke@1 978 * place.
duke@1 979 * @param name The type's name.
duke@1 980 * @param c The class to search for the member type. This is
duke@1 981 * always a superclass or implemented interface of
duke@1 982 * site's class.
duke@1 983 */
duke@1 984 Symbol findMemberType(Env<AttrContext> env,
duke@1 985 Type site,
duke@1 986 Name name,
duke@1 987 TypeSymbol c) {
duke@1 988 Symbol bestSoFar = typeNotFound;
duke@1 989 Symbol sym;
duke@1 990 Scope.Entry e = c.members().lookup(name);
duke@1 991 while (e.scope != null) {
duke@1 992 if (e.sym.kind == TYP) {
duke@1 993 return isAccessible(env, site, e.sym)
duke@1 994 ? e.sym
duke@1 995 : new AccessError(env, site, e.sym);
duke@1 996 }
duke@1 997 e = e.next();
duke@1 998 }
duke@1 999 Type st = types.supertype(c.type);
duke@1 1000 if (st != null && st.tag == CLASS) {
duke@1 1001 sym = findMemberType(env, site, name, st.tsym);
duke@1 1002 if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1003 }
duke@1 1004 for (List<Type> l = types.interfaces(c.type);
duke@1 1005 bestSoFar.kind != AMBIGUOUS && l.nonEmpty();
duke@1 1006 l = l.tail) {
duke@1 1007 sym = findMemberType(env, site, name, l.head.tsym);
duke@1 1008 if (bestSoFar.kind < AMBIGUOUS && sym.kind < AMBIGUOUS &&
duke@1 1009 sym.owner != bestSoFar.owner)
duke@1 1010 bestSoFar = new AmbiguityError(bestSoFar, sym);
duke@1 1011 else if (sym.kind < bestSoFar.kind)
duke@1 1012 bestSoFar = sym;
duke@1 1013 }
duke@1 1014 return bestSoFar;
duke@1 1015 }
duke@1 1016
duke@1 1017 /** Find a global type in given scope and load corresponding class.
duke@1 1018 * @param env The current environment.
duke@1 1019 * @param scope The scope in which to look for the type.
duke@1 1020 * @param name The type's name.
duke@1 1021 */
duke@1 1022 Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
duke@1 1023 Symbol bestSoFar = typeNotFound;
duke@1 1024 for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
duke@1 1025 Symbol sym = loadClass(env, e.sym.flatName());
duke@1 1026 if (bestSoFar.kind == TYP && sym.kind == TYP &&
duke@1 1027 bestSoFar != sym)
duke@1 1028 return new AmbiguityError(bestSoFar, sym);
duke@1 1029 else if (sym.kind < bestSoFar.kind)
duke@1 1030 bestSoFar = sym;
duke@1 1031 }
duke@1 1032 return bestSoFar;
duke@1 1033 }
duke@1 1034
duke@1 1035 /** Find an unqualified type symbol.
duke@1 1036 * @param env The current environment.
duke@1 1037 * @param name The type's name.
duke@1 1038 */
duke@1 1039 Symbol findType(Env<AttrContext> env, Name name) {
duke@1 1040 Symbol bestSoFar = typeNotFound;
duke@1 1041 Symbol sym;
duke@1 1042 boolean staticOnly = false;
duke@1 1043 for (Env<AttrContext> env1 = env; env1.outer != null; env1 = env1.outer) {
duke@1 1044 if (isStatic(env1)) staticOnly = true;
duke@1 1045 for (Scope.Entry e = env1.info.scope.lookup(name);
duke@1 1046 e.scope != null;
duke@1 1047 e = e.next()) {
duke@1 1048 if (e.sym.kind == TYP) {
duke@1 1049 if (staticOnly &&
duke@1 1050 e.sym.type.tag == TYPEVAR &&
duke@1 1051 e.sym.owner.kind == TYP) return new StaticError(e.sym);
duke@1 1052 return e.sym;
duke@1 1053 }
duke@1 1054 }
duke@1 1055
duke@1 1056 sym = findMemberType(env1, env1.enclClass.sym.type, name,
duke@1 1057 env1.enclClass.sym);
duke@1 1058 if (staticOnly && sym.kind == TYP &&
duke@1 1059 sym.type.tag == CLASS &&
duke@1 1060 sym.type.getEnclosingType().tag == CLASS &&
duke@1 1061 env1.enclClass.sym.type.isParameterized() &&
duke@1 1062 sym.type.getEnclosingType().isParameterized())
duke@1 1063 return new StaticError(sym);
duke@1 1064 else if (sym.exists()) return sym;
duke@1 1065 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1066
duke@1 1067 JCClassDecl encl = env1.baseClause ? (JCClassDecl)env1.tree : env1.enclClass;
duke@1 1068 if ((encl.sym.flags() & STATIC) != 0)
duke@1 1069 staticOnly = true;
duke@1 1070 }
duke@1 1071
duke@1 1072 if (env.tree.getTag() != JCTree.IMPORT) {
duke@1 1073 sym = findGlobalType(env, env.toplevel.namedImportScope, name);
duke@1 1074 if (sym.exists()) return sym;
duke@1 1075 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1076
duke@1 1077 sym = findGlobalType(env, env.toplevel.packge.members(), name);
duke@1 1078 if (sym.exists()) return sym;
duke@1 1079 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1080
duke@1 1081 sym = findGlobalType(env, env.toplevel.starImportScope, name);
duke@1 1082 if (sym.exists()) return sym;
duke@1 1083 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1084 }
duke@1 1085
duke@1 1086 return bestSoFar;
duke@1 1087 }
duke@1 1088
duke@1 1089 /** Find an unqualified identifier which matches a specified kind set.
duke@1 1090 * @param env The current environment.
duke@1 1091 * @param name The indentifier's name.
duke@1 1092 * @param kind Indicates the possible symbol kinds
duke@1 1093 * (a subset of VAL, TYP, PCK).
duke@1 1094 */
duke@1 1095 Symbol findIdent(Env<AttrContext> env, Name name, int kind) {
duke@1 1096 Symbol bestSoFar = typeNotFound;
duke@1 1097 Symbol sym;
duke@1 1098
duke@1 1099 if ((kind & VAR) != 0) {
duke@1 1100 sym = findVar(env, name);
duke@1 1101 if (sym.exists()) return sym;
duke@1 1102 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1103 }
duke@1 1104
duke@1 1105 if ((kind & TYP) != 0) {
duke@1 1106 sym = findType(env, name);
duke@1 1107 if (sym.exists()) return sym;
duke@1 1108 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1109 }
duke@1 1110
duke@1 1111 if ((kind & PCK) != 0) return reader.enterPackage(name);
duke@1 1112 else return bestSoFar;
duke@1 1113 }
duke@1 1114
duke@1 1115 /** Find an identifier in a package which matches a specified kind set.
duke@1 1116 * @param env The current environment.
duke@1 1117 * @param name The identifier's name.
duke@1 1118 * @param kind Indicates the possible symbol kinds
duke@1 1119 * (a nonempty subset of TYP, PCK).
duke@1 1120 */
duke@1 1121 Symbol findIdentInPackage(Env<AttrContext> env, TypeSymbol pck,
duke@1 1122 Name name, int kind) {
duke@1 1123 Name fullname = TypeSymbol.formFullName(name, pck);
duke@1 1124 Symbol bestSoFar = typeNotFound;
duke@1 1125 PackageSymbol pack = null;
duke@1 1126 if ((kind & PCK) != 0) {
duke@1 1127 pack = reader.enterPackage(fullname);
duke@1 1128 if (pack.exists()) return pack;
duke@1 1129 }
duke@1 1130 if ((kind & TYP) != 0) {
duke@1 1131 Symbol sym = loadClass(env, fullname);
duke@1 1132 if (sym.exists()) {
duke@1 1133 // don't allow programs to use flatnames
duke@1 1134 if (name == sym.name) return sym;
duke@1 1135 }
duke@1 1136 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1137 }
duke@1 1138 return (pack != null) ? pack : bestSoFar;
duke@1 1139 }
duke@1 1140
duke@1 1141 /** Find an identifier among the members of a given type `site'.
duke@1 1142 * @param env The current environment.
duke@1 1143 * @param site The type containing the symbol to be found.
duke@1 1144 * @param name The identifier's name.
duke@1 1145 * @param kind Indicates the possible symbol kinds
duke@1 1146 * (a subset of VAL, TYP).
duke@1 1147 */
duke@1 1148 Symbol findIdentInType(Env<AttrContext> env, Type site,
duke@1 1149 Name name, int kind) {
duke@1 1150 Symbol bestSoFar = typeNotFound;
duke@1 1151 Symbol sym;
duke@1 1152 if ((kind & VAR) != 0) {
duke@1 1153 sym = findField(env, site, name, site.tsym);
duke@1 1154 if (sym.exists()) return sym;
duke@1 1155 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1156 }
duke@1 1157
duke@1 1158 if ((kind & TYP) != 0) {
duke@1 1159 sym = findMemberType(env, site, name, site.tsym);
duke@1 1160 if (sym.exists()) return sym;
duke@1 1161 else if (sym.kind < bestSoFar.kind) bestSoFar = sym;
duke@1 1162 }
duke@1 1163 return bestSoFar;
duke@1 1164 }
duke@1 1165
duke@1 1166 /* ***************************************************************************
duke@1 1167 * Access checking
duke@1 1168 * The following methods convert ResolveErrors to ErrorSymbols, issuing
duke@1 1169 * an error message in the process
duke@1 1170 ****************************************************************************/
duke@1 1171
duke@1 1172 /** If `sym' is a bad symbol: report error and return errSymbol
duke@1 1173 * else pass through unchanged,
duke@1 1174 * additional arguments duplicate what has been used in trying to find the
duke@1 1175 * symbol (--> flyweight pattern). This improves performance since we
duke@1 1176 * expect misses to happen frequently.
duke@1 1177 *
duke@1 1178 * @param sym The symbol that was found, or a ResolveError.
duke@1 1179 * @param pos The position to use for error reporting.
duke@1 1180 * @param site The original type from where the selection took place.
duke@1 1181 * @param name The symbol's name.
duke@1 1182 * @param argtypes The invocation's value arguments,
duke@1 1183 * if we looked for a method.
duke@1 1184 * @param typeargtypes The invocation's type arguments,
duke@1 1185 * if we looked for a method.
duke@1 1186 */
duke@1 1187 Symbol access(Symbol sym,
duke@1 1188 DiagnosticPosition pos,
duke@1 1189 Type site,
duke@1 1190 Name name,
duke@1 1191 boolean qualified,
duke@1 1192 List<Type> argtypes,
duke@1 1193 List<Type> typeargtypes) {
duke@1 1194 if (sym.kind >= AMBIGUOUS) {
duke@1 1195 // printscopes(site.tsym.members());//DEBUG
duke@1 1196 if (!site.isErroneous() &&
duke@1 1197 !Type.isErroneous(argtypes) &&
duke@1 1198 (typeargtypes==null || !Type.isErroneous(typeargtypes)))
duke@1 1199 ((ResolveError)sym).report(log, pos, site, name, argtypes, typeargtypes);
duke@1 1200 do {
duke@1 1201 sym = ((ResolveError)sym).sym;
duke@1 1202 } while (sym.kind >= AMBIGUOUS);
duke@1 1203 if (sym == syms.errSymbol // preserve the symbol name through errors
duke@1 1204 || ((sym.kind & ERRONEOUS) == 0 // make sure an error symbol is returned
duke@1 1205 && (sym.kind & TYP) != 0))
jjg@110 1206 sym = types.createErrorType(name, qualified ? site.tsym : syms.noSymbol, sym.type).tsym;
duke@1 1207 }
duke@1 1208 return sym;
duke@1 1209 }
duke@1 1210
duke@1 1211 /** Same as above, but without type arguments and arguments.
duke@1 1212 */
duke@1 1213 Symbol access(Symbol sym,
duke@1 1214 DiagnosticPosition pos,
duke@1 1215 Type site,
duke@1 1216 Name name,
duke@1 1217 boolean qualified) {
duke@1 1218 if (sym.kind >= AMBIGUOUS)
duke@1 1219 return access(sym, pos, site, name, qualified, List.<Type>nil(), null);
duke@1 1220 else
duke@1 1221 return sym;
duke@1 1222 }
duke@1 1223
duke@1 1224 /** Check that sym is not an abstract method.
duke@1 1225 */
duke@1 1226 void checkNonAbstract(DiagnosticPosition pos, Symbol sym) {
duke@1 1227 if ((sym.flags() & ABSTRACT) != 0)
duke@1 1228 log.error(pos, "abstract.cant.be.accessed.directly",
duke@1 1229 kindName(sym), sym, sym.location());
duke@1 1230 }
duke@1 1231
duke@1 1232 /* ***************************************************************************
duke@1 1233 * Debugging
duke@1 1234 ****************************************************************************/
duke@1 1235
duke@1 1236 /** print all scopes starting with scope s and proceeding outwards.
duke@1 1237 * used for debugging.
duke@1 1238 */
duke@1 1239 public void printscopes(Scope s) {
duke@1 1240 while (s != null) {
duke@1 1241 if (s.owner != null)
duke@1 1242 System.err.print(s.owner + ": ");
duke@1 1243 for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
duke@1 1244 if ((e.sym.flags() & ABSTRACT) != 0)
duke@1 1245 System.err.print("abstract ");
duke@1 1246 System.err.print(e.sym + " ");
duke@1 1247 }
duke@1 1248 System.err.println();
duke@1 1249 s = s.next;
duke@1 1250 }
duke@1 1251 }
duke@1 1252
duke@1 1253 void printscopes(Env<AttrContext> env) {
duke@1 1254 while (env.outer != null) {
duke@1 1255 System.err.println("------------------------------");
duke@1 1256 printscopes(env.info.scope);
duke@1 1257 env = env.outer;
duke@1 1258 }
duke@1 1259 }
duke@1 1260
duke@1 1261 public void printscopes(Type t) {
duke@1 1262 while (t.tag == CLASS) {
duke@1 1263 printscopes(t.tsym.members());
duke@1 1264 t = types.supertype(t);
duke@1 1265 }
duke@1 1266 }
duke@1 1267
duke@1 1268 /* ***************************************************************************
duke@1 1269 * Name resolution
duke@1 1270 * Naming conventions are as for symbol lookup
duke@1 1271 * Unlike the find... methods these methods will report access errors
duke@1 1272 ****************************************************************************/
duke@1 1273
duke@1 1274 /** Resolve an unqualified (non-method) identifier.
duke@1 1275 * @param pos The position to use for error reporting.
duke@1 1276 * @param env The environment current at the identifier use.
duke@1 1277 * @param name The identifier's name.
duke@1 1278 * @param kind The set of admissible symbol kinds for the identifier.
duke@1 1279 */
duke@1 1280 Symbol resolveIdent(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1281 Name name, int kind) {
duke@1 1282 return access(
duke@1 1283 findIdent(env, name, kind),
duke@1 1284 pos, env.enclClass.sym.type, name, false);
duke@1 1285 }
duke@1 1286
duke@1 1287 /** Resolve an unqualified method identifier.
duke@1 1288 * @param pos The position to use for error reporting.
duke@1 1289 * @param env The environment current at the method invocation.
duke@1 1290 * @param name The identifier's name.
duke@1 1291 * @param argtypes The types of the invocation's value arguments.
duke@1 1292 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1293 */
duke@1 1294 Symbol resolveMethod(DiagnosticPosition pos,
duke@1 1295 Env<AttrContext> env,
duke@1 1296 Name name,
duke@1 1297 List<Type> argtypes,
duke@1 1298 List<Type> typeargtypes) {
mcimadamore@160 1299 Symbol sym = methodNotFound;
mcimadamore@160 1300 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1301 while (steps.nonEmpty() &&
mcimadamore@160 1302 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1303 sym.kind >= ERRONEOUS) {
mcimadamore@160 1304 sym = findFun(env, name, argtypes, typeargtypes,
mcimadamore@160 1305 steps.head.isBoxingRequired,
mcimadamore@160 1306 env.info.varArgs = steps.head.isVarargsRequired);
mcimadamore@160 1307 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1308 steps = steps.tail;
duke@1 1309 }
mcimadamore@160 1310 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160 1311 MethodResolutionPhase errPhase =
mcimadamore@160 1312 firstErroneousResolutionPhase();
mcimadamore@160 1313 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160 1314 pos, env.enclClass.sym.type, name, false, argtypes, typeargtypes);
mcimadamore@160 1315 env.info.varArgs = errPhase.isVarargsRequired;
duke@1 1316 }
duke@1 1317 return sym;
duke@1 1318 }
duke@1 1319
duke@1 1320 /** Resolve a qualified method identifier
duke@1 1321 * @param pos The position to use for error reporting.
duke@1 1322 * @param env The environment current at the method invocation.
duke@1 1323 * @param site The type of the qualifying expression, in which
duke@1 1324 * identifier is searched.
duke@1 1325 * @param name The identifier's name.
duke@1 1326 * @param argtypes The types of the invocation's value arguments.
duke@1 1327 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1328 */
duke@1 1329 Symbol resolveQualifiedMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1330 Type site, Name name, List<Type> argtypes,
duke@1 1331 List<Type> typeargtypes) {
mcimadamore@160 1332 Symbol sym = methodNotFound;
mcimadamore@160 1333 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1334 while (steps.nonEmpty() &&
mcimadamore@160 1335 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1336 sym.kind >= ERRONEOUS) {
mcimadamore@160 1337 sym = findMethod(env, site, name, argtypes, typeargtypes,
mcimadamore@160 1338 steps.head.isBoxingRequired(),
mcimadamore@160 1339 env.info.varArgs = steps.head.isVarargsRequired(), false);
mcimadamore@160 1340 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1341 steps = steps.tail;
duke@1 1342 }
jrose@267 1343 if (sym.kind >= AMBIGUOUS &&
jrose@267 1344 allowInvokedynamic &&
jrose@267 1345 (site == syms.invokeDynamicType ||
jrose@267 1346 site == syms.methodHandleType && name == names.invoke)) {
jrose@267 1347 // lookup failed; supply an exactly-typed implicit method
jrose@267 1348 sym = findImplicitMethod(env, site, name, argtypes, typeargtypes);
jrose@267 1349 env.info.varArgs = false;
jrose@267 1350 }
mcimadamore@160 1351 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160 1352 MethodResolutionPhase errPhase =
mcimadamore@160 1353 firstErroneousResolutionPhase();
mcimadamore@160 1354 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160 1355 pos, site, name, true, argtypes, typeargtypes);
mcimadamore@160 1356 env.info.varArgs = errPhase.isVarargsRequired;
duke@1 1357 }
duke@1 1358 return sym;
duke@1 1359 }
duke@1 1360
duke@1 1361 /** Resolve a qualified method identifier, throw a fatal error if not
duke@1 1362 * found.
duke@1 1363 * @param pos The position to use for error reporting.
duke@1 1364 * @param env The environment current at the method invocation.
duke@1 1365 * @param site The type of the qualifying expression, in which
duke@1 1366 * identifier is searched.
duke@1 1367 * @param name The identifier's name.
duke@1 1368 * @param argtypes The types of the invocation's value arguments.
duke@1 1369 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1370 */
duke@1 1371 public MethodSymbol resolveInternalMethod(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1372 Type site, Name name,
duke@1 1373 List<Type> argtypes,
duke@1 1374 List<Type> typeargtypes) {
duke@1 1375 Symbol sym = resolveQualifiedMethod(
duke@1 1376 pos, env, site, name, argtypes, typeargtypes);
duke@1 1377 if (sym.kind == MTH) return (MethodSymbol)sym;
duke@1 1378 else throw new FatalError(
mcimadamore@89 1379 diags.fragment("fatal.err.cant.locate.meth",
duke@1 1380 name));
duke@1 1381 }
duke@1 1382
duke@1 1383 /** Resolve constructor.
duke@1 1384 * @param pos The position to use for error reporting.
duke@1 1385 * @param env The environment current at the constructor invocation.
duke@1 1386 * @param site The type of class for which a constructor is searched.
duke@1 1387 * @param argtypes The types of the constructor invocation's value
duke@1 1388 * arguments.
duke@1 1389 * @param typeargtypes The types of the constructor invocation's type
duke@1 1390 * arguments.
duke@1 1391 */
duke@1 1392 Symbol resolveConstructor(DiagnosticPosition pos,
duke@1 1393 Env<AttrContext> env,
duke@1 1394 Type site,
duke@1 1395 List<Type> argtypes,
duke@1 1396 List<Type> typeargtypes) {
mcimadamore@160 1397 Symbol sym = methodNotFound;
mcimadamore@160 1398 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1399 while (steps.nonEmpty() &&
mcimadamore@160 1400 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1401 sym.kind >= ERRONEOUS) {
mcimadamore@160 1402 sym = resolveConstructor(pos, env, site, argtypes, typeargtypes,
mcimadamore@160 1403 steps.head.isBoxingRequired(),
mcimadamore@160 1404 env.info.varArgs = steps.head.isVarargsRequired());
mcimadamore@160 1405 methodResolutionCache.put(steps.head, sym);
mcimadamore@160 1406 steps = steps.tail;
duke@1 1407 }
mcimadamore@160 1408 if (sym.kind >= AMBIGUOUS) {//if nothing is found return the 'first' error
mcimadamore@160 1409 MethodResolutionPhase errPhase = firstErroneousResolutionPhase();
mcimadamore@160 1410 sym = access(methodResolutionCache.get(errPhase),
mcimadamore@160 1411 pos, site, names.init, true, argtypes, typeargtypes);
mcimadamore@160 1412 env.info.varArgs = errPhase.isVarargsRequired();
duke@1 1413 }
duke@1 1414 return sym;
duke@1 1415 }
duke@1 1416
duke@1 1417 /** Resolve constructor.
duke@1 1418 * @param pos The position to use for error reporting.
duke@1 1419 * @param env The environment current at the constructor invocation.
duke@1 1420 * @param site The type of class for which a constructor is searched.
duke@1 1421 * @param argtypes The types of the constructor invocation's value
duke@1 1422 * arguments.
duke@1 1423 * @param typeargtypes The types of the constructor invocation's type
duke@1 1424 * arguments.
duke@1 1425 * @param allowBoxing Allow boxing and varargs conversions.
duke@1 1426 * @param useVarargs Box trailing arguments into an array for varargs.
duke@1 1427 */
duke@1 1428 Symbol resolveConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1429 Type site, List<Type> argtypes,
duke@1 1430 List<Type> typeargtypes,
duke@1 1431 boolean allowBoxing,
duke@1 1432 boolean useVarargs) {
duke@1 1433 Symbol sym = findMethod(env, site,
duke@1 1434 names.init, argtypes,
duke@1 1435 typeargtypes, allowBoxing,
duke@1 1436 useVarargs, false);
duke@1 1437 if ((sym.flags() & DEPRECATED) != 0 &&
duke@1 1438 (env.info.scope.owner.flags() & DEPRECATED) == 0 &&
duke@1 1439 env.info.scope.owner.outermostClass() != sym.outermostClass())
duke@1 1440 chk.warnDeprecated(pos, sym);
duke@1 1441 return sym;
duke@1 1442 }
duke@1 1443
duke@1 1444 /** Resolve a constructor, throw a fatal error if not found.
duke@1 1445 * @param pos The position to use for error reporting.
duke@1 1446 * @param env The environment current at the method invocation.
duke@1 1447 * @param site The type to be constructed.
duke@1 1448 * @param argtypes The types of the invocation's value arguments.
duke@1 1449 * @param typeargtypes The types of the invocation's type arguments.
duke@1 1450 */
duke@1 1451 public MethodSymbol resolveInternalConstructor(DiagnosticPosition pos, Env<AttrContext> env,
duke@1 1452 Type site,
duke@1 1453 List<Type> argtypes,
duke@1 1454 List<Type> typeargtypes) {
duke@1 1455 Symbol sym = resolveConstructor(
duke@1 1456 pos, env, site, argtypes, typeargtypes);
duke@1 1457 if (sym.kind == MTH) return (MethodSymbol)sym;
duke@1 1458 else throw new FatalError(
mcimadamore@89 1459 diags.fragment("fatal.err.cant.locate.ctor", site));
duke@1 1460 }
duke@1 1461
duke@1 1462 /** Resolve operator.
duke@1 1463 * @param pos The position to use for error reporting.
duke@1 1464 * @param optag The tag of the operation tree.
duke@1 1465 * @param env The environment current at the operation.
duke@1 1466 * @param argtypes The types of the operands.
duke@1 1467 */
duke@1 1468 Symbol resolveOperator(DiagnosticPosition pos, int optag,
duke@1 1469 Env<AttrContext> env, List<Type> argtypes) {
duke@1 1470 Name name = treeinfo.operatorName(optag);
duke@1 1471 Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1472 null, false, false, true);
duke@1 1473 if (boxingEnabled && sym.kind >= WRONG_MTHS)
duke@1 1474 sym = findMethod(env, syms.predefClass.type, name, argtypes,
duke@1 1475 null, true, false, true);
duke@1 1476 return access(sym, pos, env.enclClass.sym.type, name,
duke@1 1477 false, argtypes, null);
duke@1 1478 }
duke@1 1479
duke@1 1480 /** Resolve operator.
duke@1 1481 * @param pos The position to use for error reporting.
duke@1 1482 * @param optag The tag of the operation tree.
duke@1 1483 * @param env The environment current at the operation.
duke@1 1484 * @param arg The type of the operand.
duke@1 1485 */
duke@1 1486 Symbol resolveUnaryOperator(DiagnosticPosition pos, int optag, Env<AttrContext> env, Type arg) {
duke@1 1487 return resolveOperator(pos, optag, env, List.of(arg));
duke@1 1488 }
duke@1 1489
duke@1 1490 /** Resolve binary operator.
duke@1 1491 * @param pos The position to use for error reporting.
duke@1 1492 * @param optag The tag of the operation tree.
duke@1 1493 * @param env The environment current at the operation.
duke@1 1494 * @param left The types of the left operand.
duke@1 1495 * @param right The types of the right operand.
duke@1 1496 */
duke@1 1497 Symbol resolveBinaryOperator(DiagnosticPosition pos,
duke@1 1498 int optag,
duke@1 1499 Env<AttrContext> env,
duke@1 1500 Type left,
duke@1 1501 Type right) {
duke@1 1502 return resolveOperator(pos, optag, env, List.of(left, right));
duke@1 1503 }
duke@1 1504
duke@1 1505 /**
duke@1 1506 * Resolve `c.name' where name == this or name == super.
duke@1 1507 * @param pos The position to use for error reporting.
duke@1 1508 * @param env The environment current at the expression.
duke@1 1509 * @param c The qualifier.
duke@1 1510 * @param name The identifier's name.
duke@1 1511 */
duke@1 1512 Symbol resolveSelf(DiagnosticPosition pos,
duke@1 1513 Env<AttrContext> env,
duke@1 1514 TypeSymbol c,
duke@1 1515 Name name) {
duke@1 1516 Env<AttrContext> env1 = env;
duke@1 1517 boolean staticOnly = false;
duke@1 1518 while (env1.outer != null) {
duke@1 1519 if (isStatic(env1)) staticOnly = true;
duke@1 1520 if (env1.enclClass.sym == c) {
duke@1 1521 Symbol sym = env1.info.scope.lookup(name).sym;
duke@1 1522 if (sym != null) {
duke@1 1523 if (staticOnly) sym = new StaticError(sym);
duke@1 1524 return access(sym, pos, env.enclClass.sym.type,
duke@1 1525 name, true);
duke@1 1526 }
duke@1 1527 }
duke@1 1528 if ((env1.enclClass.sym.flags() & STATIC) != 0) staticOnly = true;
duke@1 1529 env1 = env1.outer;
duke@1 1530 }
duke@1 1531 log.error(pos, "not.encl.class", c);
duke@1 1532 return syms.errSymbol;
duke@1 1533 }
duke@1 1534
duke@1 1535 /**
duke@1 1536 * Resolve `c.this' for an enclosing class c that contains the
duke@1 1537 * named member.
duke@1 1538 * @param pos The position to use for error reporting.
duke@1 1539 * @param env The environment current at the expression.
duke@1 1540 * @param member The member that must be contained in the result.
duke@1 1541 */
duke@1 1542 Symbol resolveSelfContaining(DiagnosticPosition pos,
duke@1 1543 Env<AttrContext> env,
duke@1 1544 Symbol member) {
duke@1 1545 Name name = names._this;
duke@1 1546 Env<AttrContext> env1 = env;
duke@1 1547 boolean staticOnly = false;
duke@1 1548 while (env1.outer != null) {
duke@1 1549 if (isStatic(env1)) staticOnly = true;
duke@1 1550 if (env1.enclClass.sym.isSubClass(member.owner, types) &&
duke@1 1551 isAccessible(env, env1.enclClass.sym.type, member)) {
duke@1 1552 Symbol sym = env1.info.scope.lookup(name).sym;
duke@1 1553 if (sym != null) {
duke@1 1554 if (staticOnly) sym = new StaticError(sym);
duke@1 1555 return access(sym, pos, env.enclClass.sym.type,
duke@1 1556 name, true);
duke@1 1557 }
duke@1 1558 }
duke@1 1559 if ((env1.enclClass.sym.flags() & STATIC) != 0)
duke@1 1560 staticOnly = true;
duke@1 1561 env1 = env1.outer;
duke@1 1562 }
duke@1 1563 log.error(pos, "encl.class.required", member);
duke@1 1564 return syms.errSymbol;
duke@1 1565 }
duke@1 1566
duke@1 1567 /**
duke@1 1568 * Resolve an appropriate implicit this instance for t's container.
duke@1 1569 * JLS2 8.8.5.1 and 15.9.2
duke@1 1570 */
duke@1 1571 Type resolveImplicitThis(DiagnosticPosition pos, Env<AttrContext> env, Type t) {
duke@1 1572 Type thisType = (((t.tsym.owner.kind & (MTH|VAR)) != 0)
duke@1 1573 ? resolveSelf(pos, env, t.getEnclosingType().tsym, names._this)
duke@1 1574 : resolveSelfContaining(pos, env, t.tsym)).type;
duke@1 1575 if (env.info.isSelfCall && thisType.tsym == env.enclClass.sym)
duke@1 1576 log.error(pos, "cant.ref.before.ctor.called", "this");
duke@1 1577 return thisType;
duke@1 1578 }
duke@1 1579
duke@1 1580 /* ***************************************************************************
duke@1 1581 * ResolveError classes, indicating error situations when accessing symbols
duke@1 1582 ****************************************************************************/
duke@1 1583
duke@1 1584 public void logAccessError(Env<AttrContext> env, JCTree tree, Type type) {
duke@1 1585 AccessError error = new AccessError(env, type.getEnclosingType(), type.tsym);
duke@1 1586 error.report(log, tree.pos(), type.getEnclosingType(), null, null, null);
duke@1 1587 }
duke@1 1588
mcimadamore@161 1589 private final LocalizedString noArgs = new LocalizedString("compiler.misc.no.args");
mcimadamore@161 1590
mcimadamore@161 1591 public Object methodArguments(List<Type> argtypes) {
mcimadamore@161 1592 return argtypes.isEmpty() ? noArgs : argtypes;
mcimadamore@161 1593 }
mcimadamore@161 1594
duke@1 1595 /** Root class for resolve errors.
duke@1 1596 * Instances of this class indicate "Symbol not found".
duke@1 1597 * Instances of subclass indicate other errors.
duke@1 1598 */
duke@1 1599 private class ResolveError extends Symbol {
duke@1 1600
duke@1 1601 ResolveError(int kind, Symbol sym, String debugName) {
duke@1 1602 super(kind, 0, null, null, null);
duke@1 1603 this.debugName = debugName;
duke@1 1604 this.sym = sym;
duke@1 1605 }
duke@1 1606
duke@1 1607 /** The name of the kind of error, for debugging only.
duke@1 1608 */
duke@1 1609 final String debugName;
duke@1 1610
duke@1 1611 /** The symbol that was determined by resolution, or errSymbol if none
duke@1 1612 * was found.
duke@1 1613 */
duke@1 1614 final Symbol sym;
duke@1 1615
duke@1 1616 /** The symbol that was a close mismatch, or null if none was found.
duke@1 1617 * wrongSym is currently set if a simgle method with the correct name, but
duke@1 1618 * the wrong parameters was found.
duke@1 1619 */
duke@1 1620 Symbol wrongSym;
duke@1 1621
duke@1 1622 /** An auxiliary explanation set in case of instantiation errors.
duke@1 1623 */
duke@1 1624 JCDiagnostic explanation;
duke@1 1625
duke@1 1626
duke@1 1627 public <R, P> R accept(ElementVisitor<R, P> v, P p) {
duke@1 1628 throw new AssertionError();
duke@1 1629 }
duke@1 1630
duke@1 1631 /** Print the (debug only) name of the kind of error.
duke@1 1632 */
duke@1 1633 public String toString() {
duke@1 1634 return debugName + " wrongSym=" + wrongSym + " explanation=" + explanation;
duke@1 1635 }
duke@1 1636
duke@1 1637 /** Update wrongSym and explanation and return this.
duke@1 1638 */
duke@1 1639 ResolveError setWrongSym(Symbol sym, JCDiagnostic explanation) {
duke@1 1640 this.wrongSym = sym;
duke@1 1641 this.explanation = explanation;
duke@1 1642 return this;
duke@1 1643 }
duke@1 1644
duke@1 1645 /** Update wrongSym and return this.
duke@1 1646 */
duke@1 1647 ResolveError setWrongSym(Symbol sym) {
duke@1 1648 this.wrongSym = sym;
duke@1 1649 this.explanation = null;
duke@1 1650 return this;
duke@1 1651 }
duke@1 1652
duke@1 1653 public boolean exists() {
duke@1 1654 switch (kind) {
duke@1 1655 case HIDDEN:
duke@1 1656 case ABSENT_VAR:
duke@1 1657 case ABSENT_MTH:
duke@1 1658 case ABSENT_TYP:
duke@1 1659 return false;
duke@1 1660 default:
duke@1 1661 return true;
duke@1 1662 }
duke@1 1663 }
duke@1 1664
duke@1 1665 /** Report error.
duke@1 1666 * @param log The error log to be used for error reporting.
duke@1 1667 * @param pos The position to be used for error reporting.
duke@1 1668 * @param site The original type from where the selection took place.
duke@1 1669 * @param name The name of the symbol to be resolved.
duke@1 1670 * @param argtypes The invocation's value arguments,
duke@1 1671 * if we looked for a method.
duke@1 1672 * @param typeargtypes The invocation's type arguments,
duke@1 1673 * if we looked for a method.
duke@1 1674 */
duke@1 1675 void report(Log log, DiagnosticPosition pos, Type site, Name name,
duke@1 1676 List<Type> argtypes, List<Type> typeargtypes) {
mcimadamore@80 1677 if (argtypes == null)
mcimadamore@80 1678 argtypes = List.nil();
mcimadamore@80 1679 if (typeargtypes == null)
mcimadamore@80 1680 typeargtypes = List.nil();
jjg@113 1681 if (name != names.error) {
mcimadamore@80 1682 KindName kindname = absentKind(kind);
mcimadamore@80 1683 Name idname = name;
duke@1 1684 if (kind >= WRONG_MTHS && kind <= ABSENT_MTH) {
duke@1 1685 if (isOperator(name)) {
duke@1 1686 log.error(pos, "operator.cant.be.applied",
mcimadamore@80 1687 name, argtypes);
duke@1 1688 return;
duke@1 1689 }
jjg@113 1690 if (name == names.init) {
mcimadamore@80 1691 kindname = KindName.CONSTRUCTOR;
mcimadamore@80 1692 idname = site.tsym.name;
duke@1 1693 }
duke@1 1694 }
duke@1 1695 if (kind == WRONG_MTH) {
mcimadamore@80 1696 Symbol ws = wrongSym.asMemberOf(site, types);
duke@1 1697 log.error(pos,
duke@1 1698 "cant.apply.symbol" + (explanation != null ? ".1" : ""),
mcimadamore@80 1699 kindname,
mcimadamore@80 1700 ws.name == names.init ? ws.owner.name : ws.name,
mcimadamore@161 1701 methodArguments(ws.type.getParameterTypes()),
mcimadamore@161 1702 methodArguments(argtypes),
mcimadamore@80 1703 kindName(ws.owner),
mcimadamore@80 1704 ws.owner.type,
duke@1 1705 explanation);
jjg@113 1706 } else if (!site.tsym.name.isEmpty()) {
duke@1 1707 if (site.tsym.kind == PCK && !site.tsym.exists())
duke@1 1708 log.error(pos, "doesnt.exist", site.tsym);
mcimadamore@80 1709 else {
mcimadamore@80 1710 String errKey = getErrorKey("cant.resolve.location",
mcimadamore@80 1711 argtypes, typeargtypes,
mcimadamore@80 1712 kindname);
mcimadamore@80 1713 log.error(pos, errKey, kindname, idname, //symbol kindname, name
mcimadamore@80 1714 typeargtypes, argtypes, //type parameters and arguments (if any)
mcimadamore@80 1715 typeKindName(site), site); //location kindname, type
mcimadamore@80 1716 }
duke@1 1717 } else {
mcimadamore@80 1718 String errKey = getErrorKey("cant.resolve",
mcimadamore@80 1719 argtypes, typeargtypes,
mcimadamore@80 1720 kindname);
mcimadamore@80 1721 log.error(pos, errKey, kindname, idname, //symbol kindname, name
mcimadamore@80 1722 typeargtypes, argtypes); //type parameters and arguments (if any)
duke@1 1723 }
duke@1 1724 }
duke@1 1725 }
mcimadamore@80 1726 //where
mcimadamore@80 1727 String getErrorKey(String key, List<Type> argtypes, List<Type> typeargtypes, KindName kindname) {
mcimadamore@80 1728 String suffix = "";
mcimadamore@80 1729 switch (kindname) {
mcimadamore@80 1730 case METHOD:
mcimadamore@80 1731 case CONSTRUCTOR: {
mcimadamore@80 1732 suffix += ".args";
mcimadamore@80 1733 suffix += typeargtypes.nonEmpty() ? ".params" : "";
mcimadamore@80 1734 }
duke@1 1735 }
mcimadamore@80 1736 return key + suffix;
mcimadamore@80 1737 }
mcimadamore@80 1738
mcimadamore@80 1739 /** A name designates an operator if it consists
mcimadamore@80 1740 * of a non-empty sequence of operator symbols +-~!/*%&|^<>=
mcimadamore@80 1741 */
mcimadamore@80 1742 boolean isOperator(Name name) {
mcimadamore@80 1743 int i = 0;
jjg@113 1744 while (i < name.getByteLength() &&
jjg@113 1745 "+-~!*/%&|^<>=".indexOf(name.getByteAt(i)) >= 0) i++;
jjg@113 1746 return i > 0 && i == name.getByteLength();
mcimadamore@80 1747 }
duke@1 1748 }
duke@1 1749
duke@1 1750 /** Resolve error class indicating that a symbol is not accessible.
duke@1 1751 */
duke@1 1752 class AccessError extends ResolveError {
duke@1 1753
duke@1 1754 AccessError(Symbol sym) {
duke@1 1755 this(null, null, sym);
duke@1 1756 }
duke@1 1757
duke@1 1758 AccessError(Env<AttrContext> env, Type site, Symbol sym) {
duke@1 1759 super(HIDDEN, sym, "access error");
duke@1 1760 this.env = env;
duke@1 1761 this.site = site;
duke@1 1762 if (debugResolve)
duke@1 1763 log.error("proc.messager", sym + " @ " + site + " is inaccessible.");
duke@1 1764 }
duke@1 1765
duke@1 1766 private Env<AttrContext> env;
duke@1 1767 private Type site;
duke@1 1768
duke@1 1769 /** Report error.
duke@1 1770 * @param log The error log to be used for error reporting.
duke@1 1771 * @param pos The position to be used for error reporting.
duke@1 1772 * @param site The original type from where the selection took place.
duke@1 1773 * @param name The name of the symbol to be resolved.
duke@1 1774 * @param argtypes The invocation's value arguments,
duke@1 1775 * if we looked for a method.
duke@1 1776 * @param typeargtypes The invocation's type arguments,
duke@1 1777 * if we looked for a method.
duke@1 1778 */
duke@1 1779 void report(Log log, DiagnosticPosition pos, Type site, Name name,
duke@1 1780 List<Type> argtypes, List<Type> typeargtypes) {
duke@1 1781 if (sym.owner.type.tag != ERROR) {
jjg@113 1782 if (sym.name == names.init && sym.owner != site.tsym)
duke@1 1783 new ResolveError(ABSENT_MTH, sym.owner, "absent method " + sym).report(
duke@1 1784 log, pos, site, name, argtypes, typeargtypes);
duke@1 1785 if ((sym.flags() & PUBLIC) != 0
duke@1 1786 || (env != null && this.site != null
duke@1 1787 && !isAccessible(env, this.site)))
duke@1 1788 log.error(pos, "not.def.access.class.intf.cant.access",
duke@1 1789 sym, sym.location());
duke@1 1790 else if ((sym.flags() & (PRIVATE | PROTECTED)) != 0)
duke@1 1791 log.error(pos, "report.access", sym,
mcimadamore@80 1792 asFlagSet(sym.flags() & (PRIVATE | PROTECTED)),
duke@1 1793 sym.location());
duke@1 1794 else
duke@1 1795 log.error(pos, "not.def.public.cant.access",
duke@1 1796 sym, sym.location());
duke@1 1797 }
duke@1 1798 }
duke@1 1799 }
duke@1 1800
duke@1 1801 /** Resolve error class indicating that an instance member was accessed
duke@1 1802 * from a static context.
duke@1 1803 */
duke@1 1804 class StaticError extends ResolveError {
duke@1 1805 StaticError(Symbol sym) {
duke@1 1806 super(STATICERR, sym, "static error");
duke@1 1807 }
duke@1 1808
duke@1 1809 /** Report error.
duke@1 1810 * @param log The error log to be used for error reporting.
duke@1 1811 * @param pos The position to be used for error reporting.
duke@1 1812 * @param site The original type from where the selection took place.
duke@1 1813 * @param name The name of the symbol to be resolved.
duke@1 1814 * @param argtypes The invocation's value arguments,
duke@1 1815 * if we looked for a method.
duke@1 1816 * @param typeargtypes The invocation's type arguments,
duke@1 1817 * if we looked for a method.
duke@1 1818 */
duke@1 1819 void report(Log log,
duke@1 1820 DiagnosticPosition pos,
duke@1 1821 Type site,
duke@1 1822 Name name,
duke@1 1823 List<Type> argtypes,
duke@1 1824 List<Type> typeargtypes) {
mcimadamore@80 1825 Symbol errSym = ((sym.kind == TYP && sym.type.tag == CLASS)
mcimadamore@80 1826 ? types.erasure(sym.type).tsym
mcimadamore@80 1827 : sym);
duke@1 1828 log.error(pos, "non-static.cant.be.ref",
mcimadamore@80 1829 kindName(sym), errSym);
duke@1 1830 }
duke@1 1831 }
duke@1 1832
duke@1 1833 /** Resolve error class indicating an ambiguous reference.
duke@1 1834 */
duke@1 1835 class AmbiguityError extends ResolveError {
duke@1 1836 Symbol sym1;
duke@1 1837 Symbol sym2;
duke@1 1838
duke@1 1839 AmbiguityError(Symbol sym1, Symbol sym2) {
duke@1 1840 super(AMBIGUOUS, sym1, "ambiguity error");
duke@1 1841 this.sym1 = sym1;
duke@1 1842 this.sym2 = sym2;
duke@1 1843 }
duke@1 1844
duke@1 1845 /** Report error.
duke@1 1846 * @param log The error log to be used for error reporting.
duke@1 1847 * @param pos The position to be used for error reporting.
duke@1 1848 * @param site The original type from where the selection took place.
duke@1 1849 * @param name The name of the symbol to be resolved.
duke@1 1850 * @param argtypes The invocation's value arguments,
duke@1 1851 * if we looked for a method.
duke@1 1852 * @param typeargtypes The invocation's type arguments,
duke@1 1853 * if we looked for a method.
duke@1 1854 */
duke@1 1855 void report(Log log, DiagnosticPosition pos, Type site, Name name,
duke@1 1856 List<Type> argtypes, List<Type> typeargtypes) {
duke@1 1857 AmbiguityError pair = this;
duke@1 1858 while (true) {
duke@1 1859 if (pair.sym1.kind == AMBIGUOUS)
duke@1 1860 pair = (AmbiguityError)pair.sym1;
duke@1 1861 else if (pair.sym2.kind == AMBIGUOUS)
duke@1 1862 pair = (AmbiguityError)pair.sym2;
duke@1 1863 else break;
duke@1 1864 }
duke@1 1865 Name sname = pair.sym1.name;
jjg@113 1866 if (sname == names.init) sname = pair.sym1.owner.name;
duke@1 1867 log.error(pos, "ref.ambiguous", sname,
duke@1 1868 kindName(pair.sym1),
duke@1 1869 pair.sym1,
duke@1 1870 pair.sym1.location(site, types),
duke@1 1871 kindName(pair.sym2),
duke@1 1872 pair.sym2,
duke@1 1873 pair.sym2.location(site, types));
duke@1 1874 }
duke@1 1875 }
mcimadamore@160 1876
mcimadamore@160 1877 enum MethodResolutionPhase {
mcimadamore@160 1878 BASIC(false, false),
mcimadamore@160 1879 BOX(true, false),
mcimadamore@160 1880 VARARITY(true, true);
mcimadamore@160 1881
mcimadamore@160 1882 boolean isBoxingRequired;
mcimadamore@160 1883 boolean isVarargsRequired;
mcimadamore@160 1884
mcimadamore@160 1885 MethodResolutionPhase(boolean isBoxingRequired, boolean isVarargsRequired) {
mcimadamore@160 1886 this.isBoxingRequired = isBoxingRequired;
mcimadamore@160 1887 this.isVarargsRequired = isVarargsRequired;
mcimadamore@160 1888 }
mcimadamore@160 1889
mcimadamore@160 1890 public boolean isBoxingRequired() {
mcimadamore@160 1891 return isBoxingRequired;
mcimadamore@160 1892 }
mcimadamore@160 1893
mcimadamore@160 1894 public boolean isVarargsRequired() {
mcimadamore@160 1895 return isVarargsRequired;
mcimadamore@160 1896 }
mcimadamore@160 1897
mcimadamore@160 1898 public boolean isApplicable(boolean boxingEnabled, boolean varargsEnabled) {
mcimadamore@160 1899 return (varargsEnabled || !isVarargsRequired) &&
mcimadamore@160 1900 (boxingEnabled || !isBoxingRequired);
mcimadamore@160 1901 }
mcimadamore@160 1902 }
mcimadamore@160 1903
mcimadamore@160 1904 private Map<MethodResolutionPhase, Symbol> methodResolutionCache =
mcimadamore@160 1905 new HashMap<MethodResolutionPhase, Symbol>(MethodResolutionPhase.values().length);
mcimadamore@160 1906
mcimadamore@160 1907 final List<MethodResolutionPhase> methodResolutionSteps = List.of(BASIC, BOX, VARARITY);
mcimadamore@160 1908
mcimadamore@160 1909 private MethodResolutionPhase firstErroneousResolutionPhase() {
mcimadamore@160 1910 MethodResolutionPhase bestSoFar = BASIC;
mcimadamore@160 1911 Symbol sym = methodNotFound;
mcimadamore@160 1912 List<MethodResolutionPhase> steps = methodResolutionSteps;
mcimadamore@160 1913 while (steps.nonEmpty() &&
mcimadamore@160 1914 steps.head.isApplicable(boxingEnabled, varargsEnabled) &&
mcimadamore@160 1915 sym.kind >= WRONG_MTHS) {
mcimadamore@160 1916 sym = methodResolutionCache.get(steps.head);
mcimadamore@160 1917 bestSoFar = steps.head;
mcimadamore@160 1918 steps = steps.tail;
mcimadamore@160 1919 }
mcimadamore@160 1920 return bestSoFar;
mcimadamore@160 1921 }
duke@1 1922 }

mercurial