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

Tue, 07 Sep 2010 17:32:27 +0100

author
mcimadamore
date
Tue, 07 Sep 2010 17:32:27 +0100
changeset 674
584365f256a7
parent 643
a626d8c1de6e
child 689
77cc34d5e548
permissions
-rw-r--r--

6979327: method handle invocation should use casts instead of type parameters to specify return type
Summary: infer return type for polymorphic signature calls according to updated JSR 292 draft
Reviewed-by: jjg
Contributed-by: john.r.rose@oracle.com

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

mercurial