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

Fri, 10 Dec 2010 15:24:17 +0000

author
mcimadamore
date
Fri, 10 Dec 2010 15:24:17 +0000
changeset 787
b1c98bfd4709
parent 775
536ee9f126b1
child 789
878c8f760ded
permissions
-rw-r--r--

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

mercurial