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

Tue, 10 Aug 2010 14:52:34 +0100

author
mcimadamore
date
Tue, 10 Aug 2010 14:52:34 +0100
changeset 631
a2d8c7071f24
parent 612
d1bd93028447
child 643
a626d8c1de6e
permissions
-rw-r--r--

6975275: diamond implementation needs some cleanup
Summary: resolution issues during diamond inference should be reported through Resolve.logResolveError()
Reviewed-by: jjg

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

mercurial