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

Sat, 18 Sep 2010 09:56:23 -0700

author
mcimadamore
date
Sat, 18 Sep 2010 09:56:23 -0700
changeset 689
77cc34d5e548
parent 674
584365f256a7
child 700
7b413ac1a720
permissions
-rw-r--r--

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

mercurial