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

Wed, 02 Mar 2011 10:56:39 +0000

author
mcimadamore
date
Wed, 02 Mar 2011 10:56:39 +0000
changeset 901
02b699d97a55
parent 880
0c24826853b2
child 913
74f0c05c51eb
permissions
-rw-r--r--

6541876: "Enclosing Instance" error new in 1.6
Summary: unqualified 'this' should not be selected in a qualified super() call in a default constructor
Reviewed-by: jjg

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

mercurial