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

Wed, 14 Apr 2010 12:31:55 +0100

author
mcimadamore
date
Wed, 14 Apr 2010 12:31:55 +0100
changeset 537
9d9d08922405
parent 325
ad07b7ea9685
child 547
04cf82179fa7
child 571
f0e3ec1f9d9f
permissions
-rw-r--r--

6939620: Switch to 'complex' diamond inference scheme
Summary: Implement new inference scheme for diamond operator that takes into account type of actual arguments supplied to constructor
Reviewed-by: jjg, darcy

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

mercurial